home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / C and C++ / Gnuplot 3.5 for Macintosh / SOURCES 3.5 / command_1.c < prev    next >
Text File  |  1993-11-12  |  121KB  |  4,510 lines

  1. #ifndef lint
  2. static char    *RCSid = "$Id: command.c%v 3.50.1.15 1993/08/21 15:23:42 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - command.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  * 
  10.  * Permission to use, copy, and distribute this software and its documentation
  11.  * for any purpose with or without fee is hereby granted, provided that the
  12.  * above copyright notice appear in all copies and that both that copyright
  13.  * notice and this permission notice appear in supporting documentation.
  14.  * 
  15.  * Permission to modify the software is granted, but not the right to distribute
  16.  * the modified code.  Modifications are to be distributed as patches to
  17.  * released version.
  18.  * 
  19.  * This software is provided "as is" without express or implied warranty.
  20.  * 
  21.  * 
  22.  * AUTHORS
  23.  * 
  24.  * Original Software: Thomas Williams,  Colin Kelley.
  25.  * 
  26.  * Gnuplot 2.0 additions: Russell Lang, Dave Kotz, John Campbell.
  27.  * 
  28.  * Gnuplot 3.0 additions: Gershon Elber and many others.
  29.  * 
  30.  * Changes:
  31.  * 
  32.  * Feb 5, 1992    Jack Veenstra    (veenstra@cs.rochester.edu) Added support to
  33.  * filter data values read from a file through a user-defined function before
  34.  * plotting. The keyword "thru" was added to the "plot" command. Example
  35.  * syntax: f(x) = x / 100 plot "test.data" thru f(x) This example divides all
  36.  * the y values by 100 before plotting. The filter function processes the
  37.  * data before any log-scaling occurs. This capability should be generalized
  38.  * to filter x values as well and a similar feature should be added to the
  39.  * "splot" command.
  40.  * 
  41.  * 19 September 1992  Lawrence Crowl  (crowl@cs.orst.edu)
  42.  * Added user-specified bases for log scaling.
  43.  * 
  44.  * There is a mailing list for gnuplot users. Note, however, that the
  45.  * newsgroup 
  46.  *    comp.graphics.gnuplot 
  47.  * is identical to the mailing list (they
  48.  * both carry the same set of messages). We prefer that you read the
  49.  * messages through that newsgroup, to subscribing to the mailing list.
  50.  * (If you can read that newsgroup, and are already on the mailing list,
  51.  * please send a message info-gnuplot-request@dartmouth.edu, asking to be
  52.  * removed from the mailing list.)
  53.  *
  54.  * The address for mailing to list members is
  55.  *       info-gnuplot@dartmouth.edu
  56.  * and for mailing administrative requests is 
  57.  *       info-gnuplot-request@dartmouth.edu
  58.  * The mailing list for bug reports is 
  59.  *       bug-gnuplot@dartmouth.edu
  60.  * The list of those interested in beta-test versions is
  61.  *       info-gnuplot-beta@dartmouth.edu
  62.  */
  63. #ifdef THINK_C
  64. #define THINK_C_1
  65. #endif
  66.  
  67. #include <stdio.h>
  68. #include <math.h>
  69. #include <ctype.h>
  70.  
  71. #ifdef AMIGA_AC_5
  72. #include <time.h>
  73. void            sleep();    /* defined later */
  74. #endif
  75.  
  76. #ifdef THINK_C
  77. #include <time.h>
  78. #include <stdlib.h>
  79. #endif
  80.  
  81. #ifdef OS2
  82. #include <setjmp.h>
  83. extern jmp_buf env;       /* from plot.c */
  84. #endif
  85.  
  86. #if defined(MSDOS) || defined(DOS386)
  87. #ifdef DJGPP
  88. #include <dos.h>
  89. #else
  90. #include <process.h>
  91. #endif
  92.  
  93. #ifdef __ZTC__
  94. #define P_WAIT 0
  95. #include <time.h>        /* usleep() */
  96. #else
  97.  
  98. #ifdef __TURBOC__
  99. #ifndef _Windows
  100. #include <dos.h>        /* sleep() */
  101. #include <conio.h>
  102. #include <dir.h>    /* setdisk() */
  103. extern unsigned _stklen = 16394;/* increase stack size */
  104. #endif
  105.  
  106. #else                /* must be MSC */
  107. #if !defined(__EMX__) && !defined(DJGPP)
  108. #ifdef __MSC__
  109. #include <direct.h>        /* for _chdrive() */
  110. #endif
  111. #include <time.h>        /* kludge to provide sleep() */
  112. void            sleep();    /* defined later */
  113. #endif                /* !__EMX__ && !DJGPP */
  114. #endif                /* TURBOC */
  115. #endif                /* ZTC */
  116.  
  117. #endif                /* MSDOS */
  118.  
  119. #ifdef ATARI
  120. #ifdef __PUREC__
  121. #include <ext.h>
  122. #include <tos.h>
  123. #include <aes.h>
  124. #else
  125. #include <osbind.h>
  126. #include <aesbind.h>
  127. #endif /* __PUREC__ */
  128. #endif /* ATARI */
  129.  
  130. #ifdef AMIGA_SC_6_1
  131. #include <proto/dos.h>
  132. void            sleep();
  133. #endif                /* AMIGA_SC_6_1 */
  134.  
  135. #include "plot.h"
  136. #ifdef THINK_C
  137. #include "tout_protos.h"
  138. #endif
  139. #include "setshow.h"
  140. #ifndef _Windows
  141. #include "help.h"
  142. #else
  143. #define MAXSTR 255
  144. #endif
  145.  
  146. #ifndef STDOUT
  147. #define STDOUT 1
  148. #endif
  149.  
  150. #ifndef HELPFILE
  151. #if defined( MSDOS ) || defined( OS2 ) || defined(DOS386)
  152. #define HELPFILE "gnuplot.gih"
  153. #else
  154. #ifdef AMIGA_SC_6_1
  155. #define HELPFILE "S:gnuplot.gih"
  156. #else
  157. #define HELPFILE "docs/gnuplot.gih"    /* changed by makefile */
  158. #endif                /* AMIGA_SC_6_1 */
  159. #endif
  160. #endif                /* HELPFILE */
  161.  
  162. #ifdef _Windows
  163. #include <windows.h>
  164. #include <setjmp.h>
  165. #ifdef __MSC__
  166. #include <malloc.h>
  167. #else
  168. #include <alloc.h>
  169. #include <dir.h>    /* setdisk() */
  170. #endif
  171. #include "win/wgnuplib.h"
  172. void sleep();
  173. extern TW textwin;
  174. extern jmp_buf FAR env;       /* from plot.c */
  175. extern LPSTR winhelpname;
  176. extern void screen_dump(void);    /* in term/win.trm */
  177. extern int Pause(LPSTR mess); /* in winmain.c */
  178. #endif
  179.  
  180. #define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
  181.  
  182. /*
  183.  * instead of <strings.h>
  184.  */
  185.  
  186. #ifndef ATARI
  187. #ifdef _Windows
  188. #include <string.h>
  189. #include <stdlib.h>
  190. #elif THINK_C
  191. #include <string.h>
  192. #include <stdlib.h>
  193. #else
  194. #ifndef AMIGA_SC_6_1
  195. extern char    *gets(), *getenv();
  196. #ifdef sequent
  197. extern char    *strcpy(), *strncpy(), *strcat(), *index();
  198. #else
  199. extern char    *strcpy(), *strncpy(), *strcat(), *strchr();
  200. #endif
  201. extern int      strlen();
  202. extern int      strcmp();
  203. extern double   atof();
  204. #endif /* !AMIGA_SC_6_1 */
  205. #endif
  206. #else
  207. #ifdef __PUREC__
  208. /*
  209.  * a substitute for PureC's buggy sscanf.
  210.  * this uses the normal sscanf and fixes the following bugs:
  211.  * - whitespace in format matches whitespace in string, but doesn't
  212.  *   require any. ( "%f , %f" scans "1,2" correctly )
  213.  * - the ignore value feature works (*). this created an address error
  214.  *   in PureC.
  215.  */
  216.  
  217. #include <stdarg.h>
  218. #include <string.h>
  219.  
  220. int purec_sscanf( const char *string, const char *format, ... )
  221. {
  222.   va_list args;
  223.   int cnt=0;
  224.   char onefmt[256];
  225.   char buffer[256];
  226.   const char *f=format;
  227.   const char *s=string;
  228.   char *f2;
  229.   char ch;
  230.   int ignore;
  231.   void *p;
  232.   int *ip;
  233.   int pos;
  234.  
  235.   va_start(args,format);
  236.   while( *f && *s ) {
  237.     ch=*f++;
  238.     if( ch!='%' ) {
  239.       if(isspace(ch)) {
  240.         /* match any number of whitespace */
  241.         while(isspace(*s)) s++;
  242.       } else {
  243.         /* match exactly the character ch */
  244.         if( *s!=ch ) goto finish;
  245.         s++;
  246.       }
  247.     } else {
  248.       /* we have got a '%' */
  249.       ch=*f++;
  250.       if( ch=='%' ) {
  251.         /* match exactly % */
  252.         if( *s!=ch ) goto finish;
  253.         s++;
  254.       } else {
  255.         f2=onefmt;
  256.         *f2++='%';
  257.         *f2++=ch;
  258.         ignore=0;
  259.         if( ch=='*' ) {
  260.           ignore=1;
  261.           ch=f2[-1]=*f++;
  262.         }
  263.         while( isdigit(ch) ) {
  264.           ch=*f2++=*f++;
  265.         }
  266.         if( ch=='l' || ch=='L' || ch=='h' ) {
  267.           ch=*f2++=*f++;
  268.         }
  269.         switch(ch) {
  270.           case '[':
  271.             while( ch && ch!=']' ) {
  272.               ch=*f2++=*f++;
  273.             }
  274.             if( !ch ) goto error;
  275.             break;
  276.           case 'e':
  277.           case 'f':
  278.           case 'g':
  279.           case 'd':
  280.           case 'o':
  281.           case 'i':
  282.           case 'u':
  283.           case 'x':
  284.           case 'c':
  285.           case 's':
  286.           case 'p':
  287.           case 'n': /* special case handled below */
  288.             break;
  289.           default:
  290.             goto error;
  291.         }
  292.         if( ch!='n' ) {
  293.           strcpy(f2,"%n");
  294.           if( ignore ) {
  295.             p=buffer;
  296.           } else {
  297.             p=va_arg(args,void *);
  298.           }
  299.           switch( sscanf( s, onefmt, p, &pos ) ) {
  300.             case EOF: goto error;
  301.             case  0 : goto finish;
  302.           }
  303.           if( !ignore ) cnt++;
  304.           s+=pos;
  305.         } else {
  306.           if( !ignore ) {
  307.             ip=va_arg(args,int *);
  308.             *ip=(int)(s-string);
  309.           }
  310.         }
  311.       }
  312.     }
  313.   }
  314.  
  315.   if( !*f ) goto finish;
  316.  
  317. error:
  318.   cnt=EOF;
  319. finish:
  320.   va_end(args);
  321.   return cnt;
  322. }
  323.  
  324. /* use the substitute now. I know this is dirty trick, but it works. */
  325. #define sscanf purec_sscanf
  326.  
  327. #endif /* __PUREC__ */
  328. #endif /* ATARI */
  329.  
  330. /*
  331.  * Only reference to contours library.
  332.  */
  333. extern struct gnuplot_contours *contour();
  334.  
  335. #ifdef OS2
  336.  /* emx has getcwd, chdir that can handle drive names */
  337. #define getcwd _getcwd2
  338. #define chdir  _chdir2
  339. #endif /* OS2 */
  340.  
  341. #if defined(unix) && !defined(hpux)
  342. #ifdef GETCWD
  343. extern char    *getcwd();    /* some Unix's use getcwd */
  344. #else
  345. extern char    *getwd();    /* most Unix's use getwd */
  346. #endif
  347. #else
  348. #ifdef DJGPP
  349. extern char    *getwd();    /* DJGPP acts like Unix here */
  350. #else
  351. extern char    *getcwd();    /* Turbo C, MSC, EMX, OS2 and VMS use getcwd */
  352. #endif
  353. #endif
  354.  
  355. #ifdef vms
  356. int             vms_vkid;    /* Virtual keyboard id */
  357. #endif
  358.  
  359. #if defined(THINK_C_1)
  360. FILE *data_fp=NULL;    /* != means file still open */
  361. TBOOLEAN more_data_fp=FALSE;  /* And this explicitly says so. */
  362. #elif defined(THINK_C_2)
  363. extern FILE *data_fp;    /* != means file still open */
  364. extern TBOOLEAN more_data_fp;  /* And this explicitly says so. */
  365. #else
  366. static FILE *data_fp=NULL;    /* != means file still open */
  367. static TBOOLEAN more_data_fp=FALSE;  /* And this explicitly says so. */
  368. #endif
  369.  
  370. #if defined(unix) || defined(PIPES)
  371. extern FILE    *popen();
  372. static TBOOLEAN  pipe_open = FALSE;
  373. #endif
  374.  
  375. extern int      chdir();
  376.  
  377. extern double   magnitude(), angle(), real(), imag();
  378. extern struct value *const_express(), *pop(), *Gcomplex();
  379. extern struct at_type *temp_at(), *perm_at();
  380. extern struct udft_entry *add_udf();
  381. extern struct udvt_entry *add_udv();
  382. extern void     squash_spaces();
  383. extern void     lower_case();
  384.  
  385. /* local functions */
  386. #ifdef THINK_C
  387. enum coord_type adjustlog();
  388. #else
  389. static enum coord_type adjustlog();
  390. #endif
  391.  
  392. extern TBOOLEAN  interactive;    /* from plot.c */
  393.  
  394. #if defined(THINK_C_1)
  395. /* input data, parsing variables */
  396. struct lexical_unit token[MAX_TOKENS];
  397. char            input_line[MAX_LINE_LEN + 1] = "";
  398. int             num_tokens, c_token;
  399. int             inline_num = 0;    /* input line number */
  400.  
  401. char            c_dummy_var[MAX_NUM_VAR][MAX_ID_LEN + 1];    /* current dummy vars */
  402.  
  403. /* the curves/surfaces of the plot */
  404. struct curve_points *first_plot = NULL;
  405. struct surface_points *first_3dplot = NULL;
  406. struct udft_entry plot_func;
  407. struct udft_entry *dummy_func;
  408.  
  409. /* jev -- for passing data thru user-defined function */
  410. struct udft_entry ydata_func;
  411.  
  412. /* support for replot command */
  413. char            replot_line[MAX_LINE_LEN + 1] = "";
  414. int      plot_token;    /* start of 'plot' command */
  415.  
  416. /* If last plot was a 3d one. */
  417. TBOOLEAN         is_3d_plot = FALSE;
  418.  
  419. #elif defined(THINK_C_2)
  420. /* input data, parsing variables */
  421. extern struct lexical_unit token[MAX_TOKENS];
  422. extern char            input_line[MAX_LINE_LEN + 1];
  423. extern int             num_tokens, c_token;
  424. extern int             inline_num;    /* input line number */
  425.  
  426. extern char            c_dummy_var[MAX_NUM_VAR][MAX_ID_LEN + 1];    /* current dummy vars */
  427.  
  428. /* the curves/surfaces of the plot */
  429. extern struct curve_points *first_plot;
  430. extern struct surface_points *first_3dplot;
  431. extern struct udft_entry plot_func;
  432. extern struct udft_entry *dummy_func;
  433.  
  434. /* jev -- for passing data thru user-defined function */
  435. extern struct udft_entry ydata_func;
  436.  
  437. /* support for replot command */
  438. extern char            replot_line[MAX_LINE_LEN + 1];
  439. extern int      plot_token;    /* start of 'plot' command */
  440.  
  441. /* If last plot was a 3d one. */
  442. extern TBOOLEAN         is_3d_plot;
  443.  
  444. #else
  445. /* input data, parsing variables */
  446. struct lexical_unit token[MAX_TOKENS];
  447. char            input_line[MAX_LINE_LEN + 1] = "";
  448. int             num_tokens, c_token;
  449. int             inline_num = 0;    /* input line number */
  450.  
  451. char            c_dummy_var[MAX_NUM_VAR][MAX_ID_LEN + 1];    /* current dummy vars */
  452.  
  453. /* the curves/surfaces of the plot */
  454. struct curve_points *first_plot = NULL;
  455. struct surface_points *first_3dplot = NULL;
  456. static struct udft_entry plot_func;
  457. struct udft_entry *dummy_func;
  458.  
  459. /* jev -- for passing data thru user-defined function */
  460. static struct udft_entry ydata_func;
  461.  
  462. /* support for replot command */
  463. char            replot_line[MAX_LINE_LEN + 1] = "";
  464. static int      plot_token;    /* start of 'plot' command */
  465.  
  466. /* If last plot was a 3d one. */
  467. TBOOLEAN         is_3d_plot = FALSE;
  468. #endif
  469.  
  470. #ifdef THINK_C_1
  471. com_line()
  472. {
  473.    if (read_line(PROMPT))
  474.        return(1);
  475.  
  476.     /* So we can flag any new output: if false at time of error, */
  477.     /* we reprint the command line before printing caret. */
  478.     /* TRUE for interactive terminals, since the command line is typed. */
  479.     /* FALSE for non-terminal stdin, so command line is printed anyway. */
  480.     /* (DFK 11/89) */
  481.     screen_ok = interactive;
  482.  
  483.     if (do_line())
  484.         return(1);
  485.      else
  486.         return(0);
  487. }
  488.  
  489.  
  490. do_line()
  491. {                /* also used in load_file */
  492.     if (is_system(input_line[0])) {
  493.     do_system();
  494.     (void) fputs("!\n", stderr);
  495.     return(0);
  496.     }
  497.     num_tokens = scanner(input_line);
  498.     c_token = 0;
  499.     while (c_token < num_tokens) {
  500.     if (command())
  501.            return(1);
  502.     if (c_token < num_tokens)    /* something after command */
  503.         if (equals(c_token, ";"))
  504.         c_token++;
  505.         else
  506.         int_error("';' expected", c_token);
  507.     }
  508.     return(0);
  509. }
  510.  
  511.  
  512.  
  513. command()
  514. {
  515.     FILE *fp, *lf_top();
  516.     int             i;
  517.     char            sv_file[MAX_LINE_LEN + 1];
  518. #if defined(__ZTC__)
  519.     unsigned dummy; /* it's a parameter needed for dos_setdrive */
  520. #endif
  521.     /* string holding name of save or load file */
  522.  
  523.     for (i = 0; i < MAX_NUM_VAR; i++)
  524.     c_dummy_var[i][0] = '\0';    /* no dummy variables */
  525.  
  526.     if (is_definition(c_token))
  527.     define();
  528.     else if (almost_equals(c_token, "h$elp") || equals(c_token, "?")) {
  529.     c_token++;
  530.     do_help();
  531.     } else if (almost_equals(c_token, "test")) {
  532.     c_token++;
  533.     test_term();
  534.     } else if (almost_equals(c_token, "scr$eendump")) {
  535.     c_token++;
  536. #ifdef _Windows
  537.     screen_dump();
  538. #else
  539.     fputs("screendump not implemented\n",stderr);
  540. #endif
  541.     } else if (almost_equals(c_token, "pa$use")) {
  542.     struct value    a;
  543.     int             stime, text = 0;
  544.     char            buf[MAX_LINE_LEN + 1];
  545.  
  546.     c_token++;
  547.     stime = (int) real(const_express(&a));
  548.     buf[0]='\0';
  549.     if (!(END_OF_COMMAND)) {
  550.         if (!isstring(c_token))
  551.         int_error("expecting string", c_token);
  552.         else {
  553.         quotel_str(buf, c_token);
  554. #ifdef _Windows
  555.         if (stime>=0)
  556. #endif
  557. #ifdef OS2
  558.                 if( strcmp(term_tbl[term].name, "pm" )!=0 || stime >=0 )
  559. #endif
  560.         (void) fprintf(stderr, "%s", buf);
  561.         text = 1;
  562.         }
  563.     }
  564.     if (stime < 0)
  565. #ifdef _Windows
  566.         {
  567.             if (!Pause(buf))
  568.                  longjmp(env, TRUE); /* bail out to command line */
  569.          }
  570. #else
  571. #ifdef OS2
  572.         if( strcmp(term_tbl[term].name, "pm" )==0 && stime < 0 )
  573.         {
  574.             int rc ;
  575.             if( (rc=PM_pause( buf ))==0 ) longjmp(env,TRUE) ;
  576.             else if( rc==2 ) { 
  577.         (void) fprintf(stderr, "%s", buf);
  578.         text = 1;
  579.                 (void) fgets(buf, MAX_LINE_LEN, stdin);
  580.                 }
  581.         }
  582. #else
  583.         (void) fgets(buf, MAX_LINE_LEN, stdin);
  584.     /* Hold until CR hit. */
  585. #endif /*OS2*/
  586. #endif
  587. #ifdef __ZTC__
  588.     if (stime > 0)
  589.         usleep((unsigned long) stime);
  590. #else
  591.     if (stime > 0)
  592.         sleep((unsigned int) stime);
  593. #endif
  594.     if (text != 0 && stime >= 0)
  595.         (void) fprintf(stderr, "\n");
  596.     c_token++;
  597.     screen_ok = FALSE;
  598.     } else if (almost_equals(c_token, "pr$int")) {
  599.     struct value    a;
  600.  
  601.     c_token++;
  602.     (void) const_express(&a);
  603.     (void) putc('\t', stderr);
  604.     disp_value(stderr, &a);
  605.     (void) putc('\n', stderr);
  606.     screen_ok = FALSE;
  607.     } else if (almost_equals(c_token, "p$lot")) {
  608.     plot_token = c_token++;
  609. #ifdef _Windows
  610.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  611. #endif
  612.     plotrequest();
  613. #ifdef _Windows
  614.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  615. #endif
  616.     } else if (almost_equals(c_token, "sp$lot")) {
  617.     plot_token = c_token++;
  618. #ifdef _Windows
  619.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  620. #endif
  621.     plot3drequest();
  622. #ifdef _Windows
  623.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  624. #endif
  625.     } else if (almost_equals(c_token, "rep$lot")) {
  626.     if (replot_line[0] == '\0')
  627.         int_error("no previous plot", c_token);
  628.     c_token++;
  629. #ifdef _Windows
  630.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  631. #endif
  632.     replotrequest();
  633. #ifdef _Windows
  634.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  635. #endif
  636.     } else if (almost_equals(c_token, "se$t"))
  637.     set_command();
  638.     else if (almost_equals(c_token, "sh$ow"))
  639.     show_command();
  640.     else if (almost_equals(c_token, "cl$ear")) {
  641.     if (!term_init) {
  642.         (*term_tbl[term].init) ();
  643.         term_init = TRUE;
  644.     }
  645.     (*term_tbl[term].graphics) ();
  646.     (*term_tbl[term].text) ();
  647.     (void) fflush(outfile);
  648.     screen_ok = FALSE;
  649.     c_token++;
  650.     } else if (almost_equals(c_token, "she$ll")) {
  651.     do_shell();
  652.     screen_ok = FALSE;
  653.     c_token++;
  654.     } else if (almost_equals(c_token, "sa$ve")) {
  655.     if (almost_equals(++c_token, "f$unctions")) {
  656.         if (!isstring(++c_token))
  657. #ifdef THINK_C
  658.             {     Point where={97,103};
  659.                 SFReply reply;
  660.                 
  661.                 SFPutFile(where,NULL, "\pUntitled",NULL, &reply);
  662.                 if( (reply.good) && (SetVol(reply.fName,reply.vRefNum) == noErr)){
  663.                     PtoCstr((char *) reply.fName);
  664.                     save_functions(fopen((char *) reply.fName,"w"));    
  665.                 }
  666.                 else{
  667.                     int_error("expecting filename",c_token);
  668.                 }
  669.             }
  670. #else
  671.         int_error("expecting filename", c_token);
  672. #endif
  673.         else {
  674.         quote_str(sv_file, c_token);
  675.         save_functions(fopen(sv_file, "w"));
  676.         }
  677.     } else if (almost_equals(c_token, "v$ariables")) {
  678.         if (!isstring(++c_token))
  679. #ifdef THINK_C
  680.             {     Point where={97,103};
  681.                 SFReply reply;
  682.                 
  683.                 SFPutFile(where,NULL, NULL,NULL, &reply);
  684.                 if( (reply.good) && (SetVol(reply.fName,reply.vRefNum) == noErr)){
  685.                     PtoCstr((char *) reply.fName);
  686.                     save_variables(fopen((char *) reply.fName,"w"));    
  687.                 }
  688.                 else{
  689.                     int_error("expecting filename",c_token);
  690.                 }
  691.             }
  692. #else
  693.         int_error("expecting filename", c_token);
  694. #endif
  695.         else {
  696.         quote_str(sv_file, c_token);
  697.         save_variables(fopen(sv_file, "w"));
  698.         }
  699.     } else if (almost_equals(c_token, "s$et")) {
  700.         if (!isstring(++c_token))
  701. #ifdef THINK_C
  702.             {     Point where={97,103};
  703.                 SFReply reply;
  704.                 
  705.                 SFPutFile(where,NULL, NULL,NULL, &reply);
  706.                 if( (reply.good) && (SetVol(reply.fName,reply.vRefNum) == noErr)){
  707.                     PtoCstr((char *) reply.fName);
  708.                     save_set(fopen((char *) reply.fName,"w"));    
  709.                 }
  710.                 else{
  711.                     int_error("expecting filename",c_token);
  712.                 }
  713.             }
  714. #else
  715.         int_error("expecting filename", c_token);
  716. #endif
  717.         else {
  718.         quote_str(sv_file, c_token);
  719.         save_set(fopen(sv_file, "w"));
  720.         }
  721.     } else if (isstring(c_token)) {
  722.         quote_str(sv_file, c_token);
  723.         save_all(fopen(sv_file, "w"));
  724.     } else {
  725. #ifdef THINK_C
  726.             Point where={97,103};
  727.             SFReply reply;
  728.             
  729.             SFPutFile(where,NULL, NULL,NULL, &reply);
  730.             if( (reply.good) && (SetVol(reply.fName,reply.vRefNum) == noErr)){
  731.                 PtoCstr((char *) reply.fName);
  732.                 save_all(fopen((char *) reply.fName,"w"));    
  733.             }
  734.             else{
  735.                 int_error(
  736.                 "filename or keyword 'functions', 'variables', or 'set' expected",
  737.                 c_token);
  738.             }
  739. #else
  740.         int_error(
  741.              "filename or keyword 'functions', 'variables', or 'set' expected",
  742.              c_token);
  743. #endif
  744.     }
  745.     c_token++;
  746.     } else if (almost_equals(c_token, "l$oad")) {
  747.     if (!isstring(++c_token))
  748. #ifdef THINK_C
  749.         {     Point where={97,103};
  750.             SFReply reply;
  751.             
  752.             SFGetFile(where,NULL, NULL, -1,NULL,NULL, &reply);
  753.             if( (reply.good) && (SetVol(reply.fName,reply.vRefNum) == noErr)){
  754.                 PtoCstr((char *) reply.fName);
  755.                 strcpy(sv_file,(char *) reply.fName);
  756.                 printf("loading  %s \n",sv_file);
  757.                 load_file(fopen((char *)reply.fName,"r"),(char *) reply.fName);    
  758.                 c_token = num_tokens = 0;
  759.             }
  760.             else
  761.                 int_error("expecting filename",c_token);
  762.         }
  763. #else
  764.         int_error("expecting filename", c_token);
  765. #endif
  766.     else {
  767.         quote_str(sv_file, c_token);
  768.         load_file(fp=fopen(sv_file, "r"), sv_file);
  769.         /* input_line[] and token[] now destroyed! */
  770.         c_token = num_tokens = 0;
  771.     }
  772.     } else if (almost_equals(c_token,"rer$ead")) {
  773.             fp = lf_top();
  774.             if (fp != (FILE *)NULL) rewind(fp);
  775.             c_token++;
  776.     } else if (almost_equals(c_token, "cd")) {
  777.     if (!isstring(++c_token))
  778.         int_error("expecting directory name", c_token);
  779.     else {
  780.         quotel_str(sv_file, c_token);
  781. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  782.         if (!((strlen(sv_file)==2) && isalpha(sv_file[0]) && (sv_file[1]==':')))
  783. #endif
  784.         if (chdir(sv_file)) {
  785.         int_error("Can't change to this directory", c_token);
  786.         }
  787. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  788.         if (isalpha(sv_file[0]) && (sv_file[1]==':')) {
  789. #ifdef ATARI
  790.         (void)Dsetdrv(toupper(sv_file[0])-'A');
  791. #endif
  792.  
  793. #if defined(__ZTC__)
  794.       (void)dos_setdrive(toupper(sv_file[0]) - 'A' + 1, &dummy);
  795. #endif
  796.  
  797. #if defined(MSDOS) && defined(__EMX__)
  798.         (void)_chdrive(toupper(sv_file[0]));
  799. #endif
  800. #if defined(__MSC__)
  801.         (void)_chdrive(toupper(sv_file[0])-'A');
  802. #endif
  803. #if (defined(MSDOS) || defined(_Windows)) && defined(__TURBOC__)
  804.         (void) setdisk(toupper(sv_file[0])-'A');
  805. #endif
  806. #ifdef DJGPP
  807.         { union REGS r;
  808.             r.h.ah = 0x0e;
  809.             r.x.dx = toupper(sv_file[0])-'A';
  810.             intdos(&r, &r);
  811.         }
  812. #endif
  813.         }
  814. #endif
  815.         c_token++;
  816.     }
  817.     } else if (almost_equals(c_token, "pwd")) {
  818. #if defined(unix) && !defined(hpux)
  819. #ifdef GETCWD
  820.     (void) getcwd(sv_file, MAX_ID_LEN);    /* some Unix's use getcwd */
  821. #else
  822.     (void) getwd(sv_file);    /* most Unix's use getwd */
  823. #endif
  824. #else
  825. #ifdef __EMX__
  826.     (void) _getcwd2(sv_file, MAX_ID_LEN);
  827. #else
  828.     /* Turbo C and VMS have getcwd() */
  829.     (void) getcwd(sv_file, MAX_ID_LEN);
  830. #endif
  831. #endif
  832. #ifdef DJGPP
  833.     { union REGS r;
  834.         r.h.ah = 0x19;
  835.         intdos(&r, &r);
  836.         fprintf(stderr, "%c:", r.h.al + 'a');
  837.     }
  838. #endif
  839.     fprintf(stderr, "%s\n", sv_file);
  840.     c_token++;
  841.     } else if (almost_equals(c_token, "ex$it") ||
  842.            almost_equals(c_token, "q$uit")) {
  843.     return(1);
  844.     } else if (!equals(c_token, ";")) {    /* null statement */
  845.     int_error("invalid command", c_token);
  846.     }
  847.     return(0);
  848. }
  849.  
  850. replotrequest()
  851. {
  852.     char            str[MAX_LINE_LEN + 1];
  853.     if (equals(c_token, "["))
  854.     int_error("cannot set range with replot", c_token);
  855.     if (!END_OF_COMMAND) {
  856.     capture(str, c_token, num_tokens - 1);
  857.     if ((strlen(str) + strlen(replot_line)) <= MAX_LINE_LEN - 1) {
  858.         (void) strcat(replot_line, ",");
  859.         (void) strcat(replot_line, str);
  860.     } else {
  861.         int_error("plot line too long with replot arguments", c_token);
  862.     }
  863.     }
  864.     (void) strcpy(input_line, replot_line);
  865.     screen_ok = FALSE;
  866.     num_tokens = scanner(input_line);
  867.     c_token = 1;        /* skip the 'plot' part */
  868.     is_3d_plot ? plot3drequest() : plotrequest();
  869. }
  870.  
  871.  
  872. plotrequest()
  873. /*
  874.  * In the parametric case we can say plot [a= -4:4] [-2:2] [-1:1] sin(a),a**2
  875.  * while in the non-parametric case we would say only plot [b= -2:2] [-1:1]
  876.  * sin(b)
  877.  */
  878. {
  879.     TBOOLEAN         changed;
  880.     int             dummy_token = -1;
  881.  
  882.     is_3d_plot = FALSE;
  883.  
  884.     if (parametric && strcmp(dummy_var[0], "u") == 0)
  885.     strcpy(dummy_var[0], "t");
  886.  
  887.     autoscale_lt = autoscale_t;
  888.     autoscale_lx = autoscale_x;
  889.     autoscale_ly = autoscale_y;
  890.  
  891.     if (!term)            /* unknown */
  892.     int_error("use 'set term' to set terminal type first", c_token);
  893.  
  894.     if (equals(c_token, "[")) {
  895.     c_token++;
  896.     if (isletter(c_token)) {
  897.         if (equals(c_token + 1, "=")) {
  898.         dummy_token = c_token;
  899.         c_token += 2;
  900.         } else {
  901.         /* oops; probably an expression with a variable. */
  902.         /* Parse it as an xmin expression. */
  903.         /* used to be: int_error("'=' expected",c_token); */
  904.         }
  905.     }
  906.     changed = parametric ? load_range(&tmin, &tmax) : load_range(&xmin, &xmax);
  907.     if (!equals(c_token, "]"))
  908.         int_error("']' expected", c_token);
  909.     c_token++;
  910.     if (changed) {
  911.         if (parametric)
  912.         autoscale_lt = FALSE;
  913.         else
  914.         autoscale_lx = FALSE;
  915.     }
  916.     }
  917.     if (parametric && equals(c_token, "[")) {    /* set optional x ranges */
  918.     c_token++;
  919.     changed = load_range(&xmin, &xmax);
  920.     if (!equals(c_token, "]"))
  921.         int_error("']' expected", c_token);
  922.     c_token++;
  923.     if (changed)
  924.         if(parametric)
  925.             autoscale_lt = FALSE;
  926.         else
  927.                 autoscale_lx = FALSE;
  928.     }
  929.     if (equals(c_token, "[")) {    /* set optional y ranges */
  930.     c_token++;
  931.     changed = load_range(&ymin, &ymax);
  932.     if (!equals(c_token, "]"))
  933.         int_error("']' expected", c_token);
  934.     c_token++;
  935.     if (changed)
  936.         autoscale_ly = FALSE;
  937.     }
  938.     /* use the default dummy variable unless changed */
  939.     if (dummy_token >= 0)
  940.     copy_str(c_dummy_var[0], dummy_token);
  941.     else
  942.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  943.  
  944.     eval_plots();
  945. }
  946.  
  947. plot3drequest()
  948. /*
  949.  * in the parametric case we would say splot [u= -Pi:Pi] [v= 0:2*Pi] [-1:1]
  950.  * [-1:1] [-1:1] sin(v)*cos(u),sin(v)*cos(u),sin(u) in the non-parametric
  951.  * case we would say only splot [x= -2:2] [y= -5:5] sin(x)*cos(y)
  952.  * 
  953.  */
  954. {
  955.     TBOOLEAN         changed;
  956.     int             dummy_token0 = -1, dummy_token1 = -1;
  957.  
  958.     is_3d_plot = TRUE;
  959.  
  960.     if (parametric && strcmp(dummy_var[0], "t") == 0) {
  961.     strcpy(dummy_var[0], "u");
  962.     strcpy(dummy_var[1], "v");
  963.     }
  964.     autoscale_lx = autoscale_x;
  965.     autoscale_ly = autoscale_y;
  966.     autoscale_lz = autoscale_z;
  967.  
  968.     if (!term)            /* unknown */
  969.     int_error("use 'set term' to set terminal type first", c_token);
  970.  
  971.     if (equals(c_token, "[")) {
  972.     c_token++;
  973.     if (isletter(c_token)) {
  974.         if (equals(c_token + 1, "=")) {
  975.         dummy_token0 = c_token;
  976.         c_token += 2;
  977.         } else {
  978.         /* oops; probably an expression with a variable. */
  979.         /* Parse it as an xmin expression. */
  980.         /* used to be: int_error("'=' expected",c_token); */
  981.         }
  982.     }
  983.     changed = parametric ? load_range(&umin, &umax) : load_range(&xmin, &xmax);
  984.     if (!equals(c_token, "]"))
  985.         int_error("']' expected", c_token);
  986.     c_token++;
  987.     if (changed)
  988.         if(parametric) 
  989.             autoscale_lu = FALSE;
  990.         else
  991.             autoscale_lx = FALSE;
  992.     }
  993.     if (equals(c_token, "[")) {
  994.     c_token++;
  995.     if (isletter(c_token)) {
  996.         if (equals(c_token + 1, "=")) {
  997.         dummy_token1 = c_token;
  998.         c_token += 2;
  999.         } else {
  1000.         /* oops; probably an expression with a variable. */
  1001.         /* Parse it as an xmin expression. */
  1002.         /* used to be: int_error("'=' expected",c_token); */
  1003.         }
  1004.     }
  1005.     changed = parametric ? load_range(&vmin, &vmax) : load_range(&ymin, &ymax);
  1006.     if (!equals(c_token, "]"))
  1007.         int_error("']' expected", c_token);
  1008.     c_token++;
  1009.     if (changed)
  1010.         if(parametric) 
  1011.             autoscale_lv = FALSE;
  1012.         else
  1013.             autoscale_ly = FALSE;
  1014.     }
  1015.     if (equals(c_token, "[")) {    /* set optional x (parametric) or z ranges */
  1016.     c_token++;
  1017.     changed = parametric ? load_range(&xmin, &xmax) : load_range(&zmin, &zmax);
  1018.     if (!equals(c_token, "]"))
  1019.         int_error("']' expected", c_token);
  1020.     c_token++;
  1021.     if (changed)
  1022.         if(parametric) 
  1023.             autoscale_lx = FALSE;
  1024.         else
  1025.             autoscale_lz = FALSE;
  1026.     }
  1027.     if (equals(c_token, "[")) {    /* set optional y ranges */
  1028.     c_token++;
  1029.     changed = load_range(&ymin, &ymax);
  1030.     if (!equals(c_token, "]"))
  1031.         int_error("']' expected", c_token);
  1032.     c_token++;
  1033.     if (changed)
  1034.         autoscale_ly = FALSE;
  1035.     }
  1036.     if (equals(c_token, "[")) {    /* set optional z ranges */
  1037.     c_token++;
  1038.     changed = load_range(&zmin, &zmax);
  1039.     if (!equals(c_token, "]"))
  1040.         int_error("']' expected", c_token);
  1041.     c_token++;
  1042.     if (changed)
  1043.         autoscale_lz = FALSE;
  1044.     }
  1045.     /* use the default dummy variable unless changed */
  1046.     if (dummy_token0 >= 0)
  1047.     copy_str(c_dummy_var[0], dummy_token0);
  1048.     else
  1049.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  1050.  
  1051.     if (dummy_token1 >= 0)
  1052.     copy_str(c_dummy_var[1], dummy_token1);
  1053.     else
  1054.     (void) strcpy(c_dummy_var[1], dummy_var[1]);
  1055.  
  1056.     eval_3dplots();
  1057. }
  1058.  
  1059.  
  1060. define()
  1061. {
  1062.     register int    start_token;/* the 1st token in the function definition */
  1063.     register struct udvt_entry *udv;
  1064.     register struct udft_entry *udf;
  1065.  
  1066.     if (equals(c_token + 1, "(")) {
  1067.     /* function ! */
  1068.     int             dummy_num = 0;
  1069.     start_token = c_token;
  1070.     do {
  1071.         c_token += 2;    /* skip to the next dummy */
  1072.         copy_str(c_dummy_var[dummy_num++], c_token);
  1073.     } while (equals(c_token + 1, ",") && (dummy_num < MAX_NUM_VAR));
  1074.     if (equals(c_token + 1, ","))
  1075.         int_error("function contains too many parameters", c_token + 2);
  1076.     c_token += 3;        /* skip (, dummy, ) and = */
  1077.     if (END_OF_COMMAND)
  1078.         int_error("function definition expected", c_token);
  1079.     udf = dummy_func = add_udf(start_token);
  1080.     if (udf->at)        /* already a dynamic a.t. there */
  1081.         free((char *) udf->at);    /* so free it first */
  1082.     if ((udf->at = perm_at()) == (struct at_type *) NULL)
  1083.         int_error("not enough memory for function", start_token);
  1084.     m_capture(&(udf->definition), start_token, c_token - 1);
  1085.     } else {
  1086.     /* variable ! */
  1087.     start_token = c_token;
  1088.     c_token += 2;
  1089.     udv = add_udv(start_token);
  1090.     (void) const_express(&(udv->udv_value));
  1091.     udv->udv_undef = FALSE;
  1092.     }
  1093. }
  1094.  
  1095. get_data(this_plot)
  1096.     struct curve_points *this_plot;
  1097. {
  1098.     register int    i, j, l_num, datum;
  1099.     int fcol[5], scol[5], ncol[5], prevmin, col;
  1100.     char  format[MAX_LINE_LEN + 1], data_file[MAX_LINE_LEN + 1],
  1101.           line[MAX_LINE_LEN + 1];
  1102.     /* conversion variables */
  1103.     int n, m, linestat, using;
  1104.     char *s;
  1105.     double val[5], v[5];
  1106.     float fval[5];    /* for use in sscanf */
  1107.  
  1108.     /* close forgotten input file (in case of a syntax error) */
  1109.     if( data_fp ) {
  1110. #if defined(unix) || defined(PIPES)
  1111.         if (pipe_open) {
  1112.             (void) pclose(data_fp);
  1113.             pipe_open = FALSE;
  1114.         } else
  1115. #endif /* unix || PIPES */
  1116.         (void) fclose(data_fp);
  1117.         data_fp=NULL;
  1118.     }
  1119.  
  1120.     quotel_str(data_file, c_token);
  1121.     this_plot->plot_type = DATA;
  1122. /*    if (parametric)
  1123.         int_error("Parametric data files not yet implemented", NO_CARET);
  1124. */
  1125. #if defined(unix) || defined(PIPES)
  1126.     if (*data_file == '<') {
  1127.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  1128.             os_error("cannot create pipe for data", c_token);
  1129.         else
  1130.             pipe_open = TRUE;
  1131.     } else
  1132. #endif /* unix || PIPES */
  1133.     if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  1134.         os_error("can't open data file", c_token);
  1135.  
  1136.     format[0] = '\0';
  1137.     for (i=0; i<5; i++)
  1138.         fcol[i] = i+1;
  1139.  
  1140.     using = 0;
  1141.     c_token++;            /* skip data file name */
  1142.  
  1143.     /* jev -- support for passing data from file thru user function */
  1144.     if (almost_equals(c_token, "thru$")) {
  1145.         c_token++;
  1146.         if (ydata_func.at)
  1147.             free(ydata_func.at);
  1148.         dummy_func = &ydata_func;
  1149.         ydata_func.at = perm_at();
  1150.     } else {
  1151.         if (ydata_func.at)
  1152.             free(ydata_func.at);
  1153.         ydata_func.at = NULL;
  1154.     }
  1155.  
  1156.     if (almost_equals(c_token,"u$sing")) {
  1157.         using = 1;
  1158.         c_token++;      /* skip "using" */
  1159.             
  1160.         if (!END_OF_COMMAND && !isstring(c_token)) {
  1161.             struct value a;
  1162.             for (i=0; i<5; i++)
  1163.                 fcol[i] = -1;
  1164.             fcol[0] = (int)magnitude(const_express(&a));
  1165.             for (i=1; equals(c_token,":") && i<5; i++) {
  1166.                 c_token++;      /* skip ":" */
  1167.                 fcol[i] = (int)magnitude(const_express(&a));
  1168.             }
  1169.         }
  1170.  
  1171.         if (!END_OF_COMMAND && isstring(c_token)) {
  1172.             quotel_str(format, c_token);
  1173.             c_token++;    /* skip format */
  1174.         }
  1175.     }
  1176.     
  1177.     /* sort fcol[] into scol[] removing duplicates */
  1178.     prevmin = 0;
  1179.     for (i=0; i<5; i++) {
  1180.         col = 10000;
  1181.         for (j=0; j<5; j++)
  1182.             if ((fcol[j]>prevmin) && (fcol[j]<col))
  1183.                 col = fcol[j];
  1184.         if (col <10000)
  1185.                prevmin = scol[i] = col;
  1186.         else
  1187.             scol[i] = 0;
  1188.     }
  1189.     /* normalise fcol[] into ncol[] */
  1190.     for (i=0; i<5; i++) {
  1191.         if (fcol[i] > 0)
  1192.             for (j=0; j<5; j++) {
  1193.                 if (fcol[i] == scol[j])
  1194.                     ncol[i] = j+1;
  1195.             }
  1196.         else if (fcol[i] == 0)
  1197.             ncol[i] = 0;
  1198.         else
  1199.             ncol[i] = -1;
  1200.     }
  1201.     /* set col to highest column number */
  1202.     col = 0;
  1203.     for (i=0; i<5; i++) 
  1204.         if (fcol[i]>col) col=fcol[i];
  1205.  
  1206.     l_num = 0;
  1207.     datum = 0;
  1208.     i = 0;
  1209.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1210.         l_num++;
  1211.         if (is_comment(line[0]))
  1212.             continue;        /* ignore comments */
  1213.         if (i >= this_plot->p_max) {
  1214.             /*
  1215.              * overflow about to occur. Extend size of points[] array. We
  1216.              * either double the size, or add 1000 points, whichever is a
  1217.              * smaller increment. Note i=p_max.
  1218.              */
  1219.             cp_extend(this_plot, i + (i < 1000 ? i : 1000));
  1220.         }
  1221.         if (!line[1]) {        /* is it blank line ? */
  1222.             /* break in data, make next point undefined */
  1223.             this_plot->points[i].type = UNDEFINED;
  1224.             i++;
  1225.             continue;
  1226.         }
  1227.         if (strlen(format) != 0) {
  1228.             /* use old sscanf if a format string was given */
  1229.             m = sscanf(line, format, &fval[0], &fval[1], &fval[2], &fval[3], &fval[4]);
  1230.             for (n=0; n<5; n++)        /* convert floats from sscanf to double */
  1231.                 val[n] = (double)fval[n];
  1232.  
  1233.             for (j=0; j<5 && fcol[j]>=0 && fcol[j]-1<m ; j++)
  1234.                 if (fcol[j])
  1235.                     v[j] = val[fcol[j]-1];
  1236.                 else
  1237.                     v[j] = datum;    /* using 0:n */
  1238.         }
  1239.         else {
  1240.             /* implement our own sscanf that skips lines with invalid data 
  1241.              * if a using statement was given */
  1242.             /* convert the array to its constituents */
  1243.             for(n=0; n<5; n++)            /* wipe the array */
  1244.                 val[n] = 0.0;
  1245.             n=0;                        /* n is column number */
  1246.             m=0;                        /* m is number of values read */
  1247.             linestat = 1;                /* linestat: 1 OK 2 bad value 0 EOL */
  1248.             s = line;
  1249.             while ((linestat == 1) && (n<col)) {
  1250.                 while (isspace(*s)) s++;
  1251.                 if (*s == '\0') {
  1252.                     linestat = 0;
  1253.                     break;
  1254.                 }
  1255.                 n++;
  1256.                 if (n == scol[m]) {
  1257.                     if (isdigit(*s) || *s=='-' || *s=='+' || *s=='.') {
  1258.                         val[m] = atof(s);
  1259.                         m++;
  1260.                     }
  1261.                     else
  1262.                         linestat = 2;    /* abort the line non-digit in req loc */
  1263.                 }
  1264.                 while ((!isspace(*s)) && (*s != '\0')) s++;
  1265.             }
  1266.  
  1267.             if (using && (linestat == 2))
  1268.                 continue;    /* skip lines where a using pattern is present and not met */
  1269.  
  1270.             for (j=0; j<5 && ncol[j]>=0 && ncol[j]-1<m ; j++)
  1271.                 if (ncol[j])
  1272.                     v[j] = val[ncol[j]-1];
  1273.                 else
  1274.                     v[j] = datum;    /* using 0:n */
  1275.         }
  1276.  
  1277.         switch (j) {
  1278.             case 1: {        /* only one number */
  1279.                 /* x is index, assign number to y */
  1280.                 v[1]=v[0];
  1281.                 v[0]=datum;
  1282.                 /* nobreak */
  1283.             }
  1284.             case 2: {        /* x, y */
  1285.                 /* ylow and yhigh are same as y */
  1286.                 datum++;
  1287.                 store2d_point(this_plot, i++, v[0], v[1], v[1], v[1], -1.0);
  1288.                 break;
  1289.             }
  1290.             case 3: {        /* x, y, ydelta */
  1291.                 /* ydelta is in the ylow variable */
  1292.                 datum++;
  1293.                 store2d_point(this_plot, i++, v[0], v[1], v[1]-v[2], v[1]+v[2], -1.0);
  1294.                 break;
  1295.             }
  1296.             case 4: {        /* x, y, ylow, yhigh */
  1297.                 datum++;
  1298.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], -1.0);
  1299.                 break;
  1300.             }
  1301.             case 5: {        /* x, y, ylow, yhigh, width */
  1302.                 datum++;
  1303.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], v[4]);
  1304.                 break;
  1305.             }
  1306.             default: {
  1307.                 (void) sprintf(line, "bad data on line %d", l_num);
  1308.                 /* close file before exiting to command level */
  1309. #if defined(unix) || defined(PIPES)
  1310.                 if (pipe_open) {
  1311.                     (void) pclose(data_fp);
  1312.                     pipe_open = FALSE;
  1313.                 } else
  1314. #endif /* unix || PIPES */
  1315.                     (void) fclose(data_fp);
  1316.                 data_fp=NULL;
  1317.                 int_error(line, c_token);
  1318.             }
  1319.         }
  1320.     }
  1321.     this_plot->p_count = i;
  1322.     cp_extend(this_plot, i);    /* shrink to fit */
  1323.  
  1324. #if defined(unix) || defined(PIPES)
  1325.     if (pipe_open) {
  1326.         (void) pclose(data_fp);
  1327.         pipe_open = FALSE;
  1328.     } else
  1329. #endif /* unix || PIPES */
  1330.     (void) fclose(data_fp);
  1331.     data_fp=NULL;
  1332. }
  1333.  
  1334.  
  1335. /* called by get_data for each point */
  1336. store2d_point(this_plot, i, x, y, ylow, yhigh, width)
  1337.     struct curve_points *this_plot;
  1338.     int             i;        /* point number */
  1339.     double          x, y;
  1340.     double          ylow, yhigh;
  1341.     double          width;
  1342. {
  1343.     struct coordinate GPHUGE *cp = &(this_plot->points[i]);
  1344.  
  1345.     /* the easy part: */
  1346.     cp->type = INRANGE;
  1347.     cp->x = x;
  1348.     cp->y = y;
  1349.     cp->ylow = ylow;
  1350.     cp->yhigh = yhigh;
  1351.     cp->z = width;
  1352.  
  1353.     /* jev -- pass data values thru user-defined function */
  1354.     if (ydata_func.at) {
  1355.     struct value    val;
  1356.  
  1357.     (void) Gcomplex(&ydata_func.dummy_values[0], y, 0.0);
  1358.     evaluate_at(ydata_func.at, &val);
  1359.     cp->y = real(&val);
  1360.  
  1361.     (void) Gcomplex(&ydata_func.dummy_values[0], ylow, 0.0);
  1362.     evaluate_at(ydata_func.at, &val);
  1363.     cp->ylow = real(&val);
  1364.  
  1365.     (void) Gcomplex(&ydata_func.dummy_values[0], yhigh, 0.0);
  1366.     evaluate_at(ydata_func.at, &val);
  1367.     cp->yhigh = real(&val);
  1368.     }
  1369.     /* Adjust for log scale. */
  1370.     if (is_log_x) {
  1371.         cp->type = adjustlog(cp->type, &(cp->x), log_base_log_x);
  1372.         (void) adjustlog(cp->type, &(cp->z), log_base_log_z);
  1373.     }
  1374.     if (is_log_y) {
  1375.     cp->type = adjustlog(cp->type, &(cp->y), log_base_log_y);
  1376.     /* Note ylow,yhigh can't affect cp->type. */
  1377.     (void) adjustlog(cp->type, &(cp->ylow), log_base_log_y);
  1378.     (void) adjustlog(cp->type, &(cp->yhigh), log_base_log_y);
  1379.     }
  1380.     /* Now adjust the xrange, or declare the point out of range */
  1381.     /*
  1382.      * The yrange is handled later, once we know whether to include ylow,
  1383.      * yhigh in the calculation. See adjust_yrange()
  1384.      */
  1385.     if (cp->type == INRANGE)
  1386.     if (autoscale_lx || inrange(x, xmin, xmax)) {
  1387.         if (autoscale_lx) {
  1388.         if (x < xmin)
  1389.             xmin = x;
  1390.         if (x > xmax)
  1391.             xmax = x;
  1392.         }
  1393.     } else {
  1394.         cp->type = OUTRANGE;
  1395.     }
  1396. }
  1397.  
  1398.  
  1399. /*
  1400.  * Adjust for log scale: take the log of the second parameter, in place, if
  1401.  * possible. If not possible, return new type for point; if possible, then
  1402.  * return old type for point.  The log is taken to the base implicit in the
  1403.  * third parameter.
  1404.  */
  1405. static enum coord_type
  1406. adjustlog(type, val, log_base_log)
  1407.     enum coord_type type;
  1408.     coordval       *val;
  1409.     double          log_base_log;
  1410. {
  1411.     if (*val < 0.0) {
  1412.     return (UNDEFINED);
  1413.     } else if (*val == 0.0) {
  1414.     *val = -VERYLARGE;
  1415.     return (OUTRANGE);
  1416.     } else {
  1417.     *val = log(*val)/log_base_log;
  1418.     return (type);
  1419.     }
  1420. }
  1421.  
  1422.  
  1423. /* now adjust the yrange, or declare the point out of range */
  1424. /* this does all points in a curve */
  1425. adjust_yrange(curve)
  1426.     struct curve_points *curve;
  1427. {
  1428.     TBOOLEAN         ebars = (curve->plot_style == ERRORBARS);
  1429.     int             npoints = curve->p_count;    /* number of points */
  1430.     coordval        y, ylow, yhigh;    /* one point value */
  1431.     struct coordinate GPHUGE *cp;    /* one coordinate */
  1432.     int             i;        /* index into points */
  1433.  
  1434.     for (i = 0; i < npoints; i++) {
  1435.     cp = &(curve->points[i]);
  1436.     if (cp->type == INRANGE) {
  1437.         y = is_log_y ? pow(base_log_y, cp->y) : cp->y;
  1438.         if ((autoscale_ly ||
  1439.         /*
  1440.          * inrange((is_log_y ? pow(base_log_y, y) : y), ymin, ymax) ||
  1441.          */
  1442.          inrange((y), ymin, ymax) ||
  1443.          polar)) {
  1444.         if (autoscale_ly) {
  1445.             if (y < ymin)
  1446.             ymin = y;
  1447.             if (y > ymax)
  1448.             ymax = y;
  1449.             if (ebars) {
  1450.             ylow = is_log_y ? pow(base_log_y, cp->ylow) : cp->ylow;
  1451.             yhigh = is_log_y ? pow(base_log_y, cp->yhigh) : cp->yhigh;
  1452.             if (ylow < ymin)
  1453.                 ymin = ylow;
  1454.             if (ylow > ymax)
  1455.                 ymax = ylow;
  1456.             if (yhigh < ymin)
  1457.                 ymin = yhigh;
  1458.             if (yhigh > ymax)
  1459.                 ymax = yhigh;
  1460.             }
  1461.         }
  1462.         } else {
  1463.         cp->type = OUTRANGE;
  1464.         }
  1465.     }
  1466.     }
  1467. }
  1468.  
  1469. grid_nongrid_data(this_plot)
  1470. struct surface_points *this_plot;
  1471. {
  1472.     int i, j, k;
  1473.     double x, y, z, w, dx, dy, xmin, xmax, ymin, ymax;
  1474.     struct iso_curve *old_iso_crvs = this_plot->iso_crvs;
  1475.     struct iso_curve *icrv, *oicrv, *oicrvs;
  1476.  
  1477.     /* Compute XY bounding box on the original data. */
  1478.     xmin = xmax = old_iso_crvs->points[0].x;
  1479.     ymin = ymax = old_iso_crvs->points[0].y;
  1480.     for (icrv = old_iso_crvs; icrv != NULL; icrv = icrv->next) {
  1481.     struct coordinate GPHUGE *points = icrv->points;
  1482.  
  1483.     for (i = 0; i < icrv->p_count; i++, points++) {
  1484.         if (xmin > points->x)
  1485.         xmin = points->x;
  1486.         if (xmax < points->x)
  1487.         xmax = points->x;
  1488.         if (ymin > points->y)
  1489.         ymin = points->y;
  1490.         if (ymax < points->y)
  1491.         ymax = points->y;
  1492.     }
  1493.     }
  1494.  
  1495.     dx = (xmax - xmin) / (dgrid3d_row_fineness - 1);
  1496.     dy = (ymax - ymin) / (dgrid3d_row_fineness - 1);
  1497.  
  1498.     /* Create the new grid structure, and compute the low pass filtering from
  1499.      * non grid to grid structure.
  1500.      */
  1501.     this_plot->iso_crvs = NULL;
  1502.     this_plot->num_iso_read = dgrid3d_col_fineness;
  1503.     this_plot->has_grid_topology = TRUE;
  1504.     for (i = 0, x = xmin; i < dgrid3d_col_fineness; i++, x += dx) {
  1505.     struct coordinate GPHUGE *points;
  1506.  
  1507.     icrv = iso_alloc(dgrid3d_row_fineness + 1);
  1508.     icrv->p_count = dgrid3d_row_fineness;
  1509.     icrv->next = this_plot->iso_crvs;
  1510.     this_plot->iso_crvs = icrv;
  1511.     points = icrv->points;
  1512.  
  1513.     for (j = 0, y = ymin; j < dgrid3d_row_fineness; j++, y += dy, points++) {
  1514.         z = w = 0.0;
  1515.  
  1516.         for (oicrv = old_iso_crvs; oicrv != NULL; oicrv = oicrv->next) {
  1517.         struct coordinate GPHUGE *opoints = oicrv->points;
  1518.         for (k = 0; k < oicrv->p_count; k++, opoints++) {
  1519.             double dist,
  1520.                dist_x = fabs( opoints->x - x ),
  1521.                dist_y = fabs( opoints->y - y );
  1522.  
  1523.             switch (dgrid3d_norm_value) {
  1524.             case 1:
  1525.                 dist = dist_x + dist_y;
  1526.                 break;
  1527.             case 2:
  1528.                 dist = dist_x * dist_x + dist_y * dist_y;
  1529.                 break;
  1530.             case 4:
  1531.                 dist = dist_x * dist_x + dist_y * dist_y;
  1532.                 dist *= dist;
  1533.                 break;
  1534.             case 8:
  1535.                 dist = dist_x * dist_x + dist_y * dist_y;
  1536.                 dist *= dist;
  1537.                 dist *= dist;
  1538.                 break;
  1539.             case 16:
  1540.                 dist = dist_x * dist_x + dist_y * dist_y;
  1541.                 dist *= dist;
  1542.                 dist *= dist;
  1543.                 dist *= dist;
  1544.                 break;
  1545.             default:
  1546.                 dist = pow( dist_x, dgrid3d_norm_value ) +
  1547.                    pow( dist_y, dgrid3d_norm_value );
  1548.                 break;
  1549.             }
  1550.  
  1551.             /* The weight of this point is inverse proportional
  1552.              * to the distance.
  1553.              */
  1554.             if ( dist == 0.0 )
  1555. #ifndef AMIGA_SC_6_1
  1556.             dist = VERYLARGE;
  1557. #else /* AMIGA_SC_6_1 */
  1558.             /* Multiplying VERYLARGE by opoints->z below
  1559.              * might yield Inf (i.e. a number that can't
  1560.              * be represented on the machine). This will
  1561.              * result in points->z being set to NaN. It's
  1562.              * better to have a pretty large number that is
  1563.              * also on the safe side... The numbers that are
  1564.              * read by gnuplot are float values anyway, so
  1565.              * they can't be bigger than FLT_MAX. So setting
  1566.              * dist to FLT_MAX^2 will make dist pretty large
  1567.              * with respect to any value that has been read. */
  1568.             dist = ((double)FLT_MAX)*((double)FLT_MAX);
  1569. #endif /* AMIGA_SC_6_1 */
  1570.             else
  1571.             dist = 1.0 / dist;
  1572.  
  1573.             z += opoints->z * dist;
  1574.             w += dist;
  1575.         }
  1576.         }
  1577.  
  1578.         points->x = x;
  1579.         points->y = y;
  1580.         points->z = z / w;
  1581.         points->type = INRANGE;
  1582.     }
  1583.     }
  1584.     
  1585.     /* Delete the old non grid data. */
  1586.     for (oicrvs = old_iso_crvs; oicrvs != NULL;) {
  1587.     oicrv = oicrvs;
  1588.     oicrvs = oicrvs->next;
  1589.     iso_free(oicrv);
  1590.     }
  1591. }
  1592.  
  1593. get_3ddata(this_plot)
  1594.     struct surface_points *this_plot;
  1595. {
  1596.     register int    i, j, l_num, xdatum, ydatum;
  1597.     float           x, y, z;
  1598.     char            data_file[MAX_LINE_LEN + 1], line[MAX_LINE_LEN + 1];
  1599.     char           *float_format = "%f", *float_skip = "%*f";
  1600.     static TBOOLEAN  only_z = FALSE, using_format = FALSE;
  1601.     static int      xcol = 1, ycol = 2, zcol = 3, index = -1;
  1602.     static char     format[MAX_LINE_LEN + 1];
  1603.     int            pt_in_iso_crv = 0, maxcol, num_col;
  1604.     enum XYZ_order_type {
  1605.     XYZ, YXZ, ZXY, XZY, ZYX, YZX, XY, YX
  1606.     }               xyz_order;
  1607.     struct iso_curve *this_iso;
  1608.  
  1609.     /* close forgotten file (in case of a syntax error) */
  1610.     if (data_fp && !more_data_fp) {
  1611. #if defined(unix) || defined(PIPES)
  1612.     if (pipe_open) {
  1613.         (void) pclose(data_fp);
  1614.         pipe_open = FALSE;
  1615.     } else
  1616. #endif /* unix || PIPES */
  1617.         (void) fclose(data_fp);
  1618.         data_fp=NULL;
  1619.         }
  1620.  
  1621.     quotel_str(data_file, c_token);
  1622.     this_plot->plot_type = DATA3D;
  1623.     this_plot->has_grid_topology = TRUE;
  1624.     if (!more_data_fp) {
  1625. #if defined(unix) || defined(PIPES)
  1626.     if (*data_file == '<') {
  1627.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  1628.         os_error("cannot create pipe; output not changed", c_token);
  1629.         else
  1630.         pipe_open = TRUE;
  1631.     } else
  1632. #endif /* unix || PIPES */
  1633.         if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  1634.         os_error("can't open data file", c_token);
  1635.  
  1636.     /* Initialize defualt values. */
  1637.     only_z = FALSE;
  1638.     using_format = FALSE;
  1639.     xcol = 1;
  1640.     ycol = 2;
  1641.     zcol = 3;
  1642.     index = -1;
  1643.     format[0] = '\0';
  1644.  
  1645.     c_token++;            /* skip data file name */
  1646.     if (almost_equals(c_token, "i$ndex")) {
  1647.         struct value a;
  1648.         c_token++;        /* skip "index" */
  1649.         index = (int) magnitude(const_express(&a));
  1650.     }
  1651.     if (almost_equals(c_token, "u$sing")) {
  1652.         c_token++;        /* skip "using" */
  1653.         if (!END_OF_COMMAND && !isstring(c_token)) {
  1654.         struct value a;
  1655.         zcol = (int) magnitude(const_express(&a));
  1656.         only_z = TRUE;
  1657.         if (equals(c_token, ":")) {
  1658.             c_token++;    /* skip ":" */
  1659.             only_z = FALSE;
  1660.             ycol = zcol;
  1661.             zcol = (int) magnitude(const_express(&a));
  1662.             if (equals(c_token, ":")) {
  1663.             c_token++;    /* skip ":" */
  1664.             xcol = ycol;
  1665.             ycol = zcol;
  1666.             zcol = (int) magnitude(const_express(&a));
  1667.             } else {
  1668.             if (mapping3d == MAP3D_CARTESIAN)
  1669.                 int_error("Must specify 1 or 3 columns", c_token);
  1670.             xcol = ycol;
  1671.             ycol = zcol;
  1672.             }
  1673.         }
  1674.         if (!only_z)
  1675.             if ((xcol == ycol) || (ycol == zcol) || (xcol == zcol))
  1676.             int_error("Columns must be distinct", c_token);
  1677.         }
  1678.         if (!END_OF_COMMAND && isstring(c_token)) {
  1679.         quotel_str(format, c_token);
  1680.         using_format = TRUE;
  1681.         c_token++;        /* skip format */
  1682.         }
  1683.     } else {
  1684.         if ( (only_z = !parametric) != FALSE)
  1685.         zcol = 1;
  1686.     }
  1687.     }
  1688.  
  1689.     switch (mapping3d) {
  1690.     case MAP3D_CARTESIAN:
  1691.     maxcol = (xcol > ycol) ? xcol : ycol;
  1692.     maxcol = (maxcol > zcol) ? maxcol : zcol;
  1693.     if (!only_z) {        /* Determine ordering of input columns */
  1694.         if (zcol == maxcol) {
  1695.         if (xcol < ycol)
  1696.             xyz_order = XYZ;    /* scanf(x,y,z) */
  1697.         else
  1698.             xyz_order = YXZ;    /* scanf(y,x,z) */
  1699.         } else if (ycol == maxcol) {
  1700.         if (xcol < zcol)
  1701.             xyz_order = XZY;    /* scanf(x,z,y) */
  1702.         else
  1703.             xyz_order = ZXY;    /* scanf(z,x,y) */
  1704.         } else {
  1705.         if (ycol < zcol)
  1706.             xyz_order = YZX;    /* scanf(y,z,x) */
  1707.         else
  1708.             xyz_order = ZYX;    /* scanf(z,y,x) */
  1709.         }
  1710.     }
  1711.     if (strlen(format) == 0) {
  1712.         if (only_z) {
  1713.         for (i = 1; i <= zcol; i++)
  1714.             if (i == zcol)
  1715.             (void) strcat(format, float_format);
  1716.             else
  1717.             (void) strcat(format, float_skip);
  1718.         } else {
  1719.         for (i = 1; i <= maxcol; i++)
  1720.             if ((i == xcol) || (i == ycol) || (i == zcol))
  1721.             (void) strcat(format, float_format);
  1722.             else
  1723.             (void) strcat(format, float_skip);
  1724.         }
  1725.     }
  1726.     break;
  1727.     case MAP3D_SPHERICAL:
  1728.     case MAP3D_CYLINDRICAL:
  1729.     if (only_z)
  1730.         int_error("Two or three columns for spherical/cylindrical coords.", c_token);
  1731.     maxcol = (xcol > ycol) ? xcol : ycol;
  1732.     maxcol = (maxcol > zcol) ? maxcol : zcol;
  1733.     xyz_order = (xcol < ycol) ? XY : YX;
  1734.     for (i = 1; i <= maxcol; i++)
  1735.         if ((i == xcol) || (i == ycol))
  1736.         (void) strcat(format, float_format);
  1737.         else
  1738.         (void) strcat(format, float_skip);
  1739.     }
  1740.  
  1741.     l_num = 0;
  1742.     xdatum = 0;
  1743.     ydatum = 0;
  1744.     this_plot->num_iso_read = 0;
  1745.     this_plot->has_grid_topology = TRUE;
  1746.     if (this_plot->iso_crvs != NULL) {
  1747.     struct iso_curve *icrv, *icrvs = this_plot->iso_crvs;
  1748.  
  1749.     while (icrvs) {
  1750.         icrv = icrvs;
  1751.         icrvs = icrvs->next;
  1752.         iso_free(icrv);
  1753.     }
  1754.     this_plot->iso_crvs = NULL;
  1755.     }
  1756.     if (!more_data_fp && is_binary_file(data_fp)) {    /* MOD--RKC */
  1757. #if defined(MSDOS)||defined(ATARI)||defined(OS2)||defined(_Windows)||defined(DOS386)
  1758.     /* file must be opened with binary flag. the old cr/lf problem again */
  1759. #ifdef PIPES
  1760.     if( pipe_open ) {
  1761.       pclose(data_fp);
  1762.       data_fp=NULL;
  1763.       pipe_open=FALSE;
  1764.       int_error("binary data from pipes is not implemented", NO_CARET);
  1765.     }
  1766. #endif
  1767.     data_fp = freopen(data_file, "rb", data_fp);
  1768. #endif
  1769.     xdatum = get_binary_data(this_plot, data_fp, &this_iso);
  1770.     } else {
  1771.     int last_line_blank = FALSE;
  1772.  
  1773.     more_data_fp = FALSE;
  1774.  
  1775.     this_iso = iso_alloc(samples);
  1776.  
  1777.     if (index > 0) { /* Skip data meshes until mesh index is reached. */
  1778.         int i = index;
  1779.  
  1780.         while (i--) {
  1781.         while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1782.             l_num++;
  1783.  
  1784.             if (!line[1]) {    /* is it blank line ? */
  1785.             if (last_line_blank) /* Two consecutive blanks. */
  1786.                 break;
  1787.             last_line_blank = TRUE;
  1788.             }
  1789.             else
  1790.             last_line_blank = FALSE;
  1791.         }
  1792.         if (feof(data_fp))
  1793.             int_error("mesh index overflow", NO_CARET);
  1794.         }
  1795.     }   /* end of index skip */
  1796.     
  1797.     last_line_blank = FALSE;
  1798.  
  1799.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1800.         l_num++;
  1801.         if (is_comment(line[0]))
  1802.         continue;    /* ignore comments */
  1803.         if (!line[1]) {    /* is it blank line ? */
  1804.         if (last_line_blank) { /* Two consecutive blank lines. */
  1805.             more_data_fp = TRUE;
  1806.             break;
  1807.         }
  1808.         last_line_blank = TRUE;
  1809.  
  1810.         if (pt_in_iso_crv == 0) {
  1811.             if (xdatum == 0)
  1812.             continue;
  1813.             pt_in_iso_crv = xdatum;
  1814.         }
  1815.         if (xdatum > 0) {
  1816.             this_iso->p_count = xdatum;
  1817.             this_iso->next = this_plot->iso_crvs;
  1818.             this_plot->iso_crvs = this_iso;
  1819.             this_plot->num_iso_read++;
  1820.  
  1821.             if (xdatum != pt_in_iso_crv)
  1822.             this_plot->has_grid_topology = FALSE;
  1823.  
  1824.             this_iso = iso_alloc(pt_in_iso_crv);
  1825.             xdatum = 0;
  1826.             ydatum++;
  1827.         }
  1828.         continue;
  1829.         }
  1830.         last_line_blank = FALSE;
  1831.         if (xdatum >= this_iso->p_max) {
  1832.         /*
  1833.          * overflow about to occur. Extend size of points[] array. We
  1834.          * either double the size, or add 1000 points, whichever is a
  1835.          * smaller increment. Note i=p_max.
  1836.          */
  1837.         iso_extend(this_iso,
  1838.                xdatum + (xdatum < 1000 ? xdatum : 1000));
  1839.         }
  1840.         switch (num_col = sscanf(line, format, &x, &y, &z)) {
  1841.         case 3:        /* All parameter are specified. */
  1842.         if (!only_z || using_format) {
  1843.             switch (xyz_order) {
  1844.             case XYZ:    /* scanf(x,y,z) */
  1845.             this_iso->points[xdatum].x = x;
  1846.             this_iso->points[xdatum].y = y;
  1847.             this_iso->points[xdatum].z = z;
  1848.             break;
  1849.             case XZY:    /* scanf(x,z,y) */
  1850.             this_iso->points[xdatum].x = x;
  1851.             this_iso->points[xdatum].y = z;
  1852.             this_iso->points[xdatum].z = y;
  1853.             break;
  1854.             case YXZ:    /* scanf(y,x,z) */
  1855.             this_iso->points[xdatum].x = y;
  1856.             this_iso->points[xdatum].y = x;
  1857.             this_iso->points[xdatum].z = z;
  1858.             break;
  1859.             case ZXY:    /* scanf(z,x,y) */
  1860.             this_iso->points[xdatum].x = y;
  1861.             this_iso->points[xdatum].y = z;
  1862.             this_iso->points[xdatum].z = x;
  1863.             break;
  1864.             case YZX:    /* scanf(y,z,x) */
  1865.             this_iso->points[xdatum].x = z;
  1866.             this_iso->points[xdatum].y = x;
  1867.             this_iso->points[xdatum].z = y;
  1868.             break;
  1869.             case ZYX:    /* scanf(z,y,x) */
  1870.             this_iso->points[xdatum].x = z;
  1871.             this_iso->points[xdatum].y = y;
  1872.             this_iso->points[xdatum].z = x;
  1873.             break;
  1874.             }
  1875.             if (xyz_order != XYZ) {
  1876.             x = this_iso->points[xdatum].x;
  1877.             y = this_iso->points[xdatum].y;
  1878.             z = this_iso->points[xdatum].z;
  1879.             }
  1880.             if (!parametric)
  1881.             int_error("Must be in parametric mode.",
  1882.                   NO_CARET);
  1883.             break;
  1884.         }
  1885.         case 1:        /* only one number on the line */
  1886.         if (!only_z && !using_format)
  1887.             int_error("3 columns expected, only 1 found", c_token);
  1888.         /* assign that number to z */
  1889.         this_iso->points[xdatum].z = x;
  1890.         z = x;
  1891.         this_iso->points[xdatum].x = xdatum;
  1892.         x = this_iso->points[xdatum].x;
  1893.         this_iso->points[xdatum].y = ydatum;
  1894.         y = this_iso->points[xdatum].y;
  1895.         if (parametric)
  1896.             int_error("Must be in non parametric mode.",
  1897.                   NO_CARET);
  1898.         break;
  1899.         case 2:
  1900.         switch (xyz_order) {
  1901.         case YX:
  1902.             z = x;    /* Use z as temp */
  1903.             x = y;
  1904.             y = z;
  1905.             break;
  1906.         default:
  1907.             break;
  1908.         }
  1909.         switch (mapping3d) {
  1910.         case MAP3D_CARTESIAN:
  1911.             int_error("2 columns found, 1 or 3 expected",
  1912.                   c_token);
  1913.             break;
  1914.         case MAP3D_SPHERICAL:
  1915.             if (angles_format == ANGLES_DEGREES) {
  1916.             x *= DEG2RAD;    /* Convert to radians. */
  1917.             y *= DEG2RAD;
  1918.             }
  1919.             if( num_col == 2) z = 1.0;
  1920.             this_iso->points[xdatum].x = z*cos(x) * cos(y);
  1921.             this_iso->points[xdatum].y = z*sin(x) * cos(y);
  1922.             this_iso->points[xdatum].z = z*sin(y);
  1923.             break;
  1924.         case MAP3D_CYLINDRICAL:
  1925.             if (angles_format == ANGLES_DEGREES)
  1926.             x *= DEG2RAD;    /* Convert to radians. */
  1927.             if( num_col == 2) z = 1.0;
  1928.             this_iso->points[xdatum].x = z*cos(x);
  1929.             this_iso->points[xdatum].y = z*sin(x);
  1930.             this_iso->points[xdatum].z = y;
  1931.             break;
  1932.         }
  1933.         x = this_iso->points[xdatum].x;
  1934.         y = this_iso->points[xdatum].y;
  1935.         z = this_iso->points[xdatum].z;
  1936.         break;
  1937.         default:
  1938.         (void) sprintf(line, "bad data on line %d", l_num);
  1939.         int_error(line, c_token);
  1940.         }
  1941.  
  1942.         if (is_log_x) {
  1943.         if (x <= 0.0)
  1944.             int_error("X value must be above 0 for log scale!",
  1945.                   NO_CARET);
  1946.         else
  1947.             this_iso->points[xdatum].x =
  1948.             log(this_iso->points[xdatum].x)/log_base_log_x;
  1949.         }
  1950.         if (is_log_y) {
  1951.         if (y <= 0.0)
  1952.             int_error("Y value must be above 0 for log scale!",
  1953.                   NO_CARET);
  1954.         else
  1955.             this_iso->points[xdatum].y =
  1956.             log(this_iso->points[xdatum].y)/log_base_log_y;
  1957.         }
  1958.         if (is_log_z) {
  1959.         if (z <= 0.0)
  1960.             int_error("Z value must be above 0 for log scale!",
  1961.                   NO_CARET);
  1962.         else
  1963.             this_iso->points[xdatum].z =
  1964.             log(this_iso->points[xdatum].z)/log_base_log_z;
  1965.         }
  1966.         if (autoscale_lx) {
  1967.         if (x < xmin)
  1968.             xmin = x;
  1969.         if (x > xmax)
  1970.             xmax = x;
  1971.         }
  1972.         if (autoscale_ly) {
  1973.         if (y < ymin)
  1974.             ymin = y;
  1975.         if (y > ymax)
  1976.             ymax = y;
  1977.         }
  1978.         if (autoscale_lz) {
  1979.         if (z < zmin)
  1980.             zmin = z;
  1981.         if (z > zmax)
  1982.             zmax = z;
  1983.         }
  1984.         xdatum++;    
  1985.     }  /* end of while loop */
  1986.  
  1987.     if (xdatum > 0) {
  1988.         this_plot->num_iso_read++;    /* Update last iso. */
  1989.         this_iso->p_count = xdatum;
  1990.  
  1991.         this_iso->next = this_plot->iso_crvs;
  1992.         this_plot->iso_crvs = this_iso;
  1993.  
  1994.         if (xdatum != pt_in_iso_crv)
  1995.         this_plot->has_grid_topology = FALSE;
  1996.  
  1997.     } else {
  1998.         iso_free(this_iso);    /* Free last allocation. */
  1999.     }
  2000.     }                /* MOD-RKC else of binary */
  2001.  
  2002.     if (index >= 0) more_data_fp = FALSE; /* Only one data set please. */
  2003.  
  2004.     if (!more_data_fp) {
  2005.       if (this_plot->num_iso_read <= 1)
  2006.       this_plot->has_grid_topology = FALSE;
  2007. #if defined(unix) || defined(PIPES)
  2008.       if (pipe_open) {
  2009.     if (this_plot->has_grid_topology && !hidden3d) {
  2010.       (void) pclose(data_fp);
  2011.       pipe_open = FALSE;
  2012.     }
  2013.       } else 
  2014. #endif /* unix || PIPES */
  2015.       {
  2016.     (void) fclose(data_fp);  
  2017.     data_fp = NULL;
  2018.       }
  2019.     }
  2020.  
  2021.     if (dgrid3d) grid_nongrid_data(this_plot);
  2022.  
  2023.     if (this_plot->num_iso_read <= 1)
  2024.     this_plot->has_grid_topology = FALSE;
  2025.     if (this_plot->has_grid_topology && !hidden3d) {
  2026.     struct iso_curve *new_icrvs = NULL;
  2027.     int             num_new_iso = this_plot->iso_crvs->p_count, len_new_iso = this_plot->num_iso_read;
  2028.  
  2029.     /* Now we need to set the other direction (pseudo) isolines. */
  2030.     for (i = 0; i < num_new_iso; i++) {
  2031.         struct iso_curve *new_icrv = iso_alloc(len_new_iso);
  2032.  
  2033.         new_icrv->p_count = len_new_iso;
  2034.  
  2035.         for (j = 0, this_iso = this_plot->iso_crvs;
  2036.          this_iso != NULL;
  2037.          j++, this_iso = this_iso->next) {
  2038.         new_icrv->points[j].x = this_iso->points[i].x;
  2039.         new_icrv->points[j].y = this_iso->points[i].y;
  2040.         new_icrv->points[j].z = this_iso->points[i].z;
  2041.         }
  2042.  
  2043.         new_icrv->next = new_icrvs;
  2044.         new_icrvs = new_icrv;
  2045.     }
  2046.  
  2047.     /* Append the new iso curves after the read ones. */
  2048.     for (this_iso = this_plot->iso_crvs;
  2049.          this_iso->next != NULL;
  2050.          this_iso = this_iso->next);
  2051.         this_iso->next = new_icrvs;
  2052.     }
  2053. }
  2054.  
  2055. /*
  2056.  * print_points: a debugging routine to print out the points of a curve, and
  2057.  * the curve structure. If curve<0, then we print the list of curves.
  2058.  */
  2059.  
  2060. static char    *plot_type_names[4] =
  2061. {
  2062.     "Function", "Data", "3D Function", "3d data"
  2063. };
  2064. static char    *plot_style_names[6] =
  2065. {
  2066.     "Lines", "Points", "Impulses", "LinesPoints", "Dots", "Errorbars"
  2067. };
  2068.  
  2069. print_points(curve)
  2070.     int             curve;    /* which curve to print */
  2071. {
  2072.     register struct curve_points *this_plot;
  2073.     int             i;
  2074.  
  2075.     if (curve < 0) {
  2076.     for (this_plot = first_plot, i = 0;
  2077.          this_plot != NULL;
  2078.          i++, this_plot = this_plot->next_cp) {
  2079.         printf("Curve %d:\n", i);
  2080.         if ((int) this_plot->plot_type >= 0 && (int) (this_plot->plot_type) < 4)
  2081.         printf("Plot type %d: %s\n", (int) (this_plot->plot_type),
  2082.                plot_type_names[(int) (this_plot->plot_type)]);
  2083.         else
  2084.         printf("Plot type %d: BAD\n", (int) (this_plot->plot_type));
  2085.         if ((int) this_plot->plot_style >= 0 && (int) (this_plot->plot_style) < 6)
  2086.         printf("Plot style %d: %s\n", (int) (this_plot->plot_style),
  2087.                plot_style_names[(int) (this_plot->plot_style)]);
  2088.         else
  2089.         printf("Plot style %d: BAD\n", (int) (this_plot->plot_style));
  2090.         printf("Plot title: '%s'\n", this_plot->title);
  2091.         printf("Line type %d\n", this_plot->line_type);
  2092.         printf("Point type %d\n", this_plot->point_type);
  2093.         printf("max points %d\n", this_plot->p_max);
  2094.         printf("current points %d\n", this_plot->p_count);
  2095.         printf("\n");
  2096.     }
  2097.     } else {
  2098.     for (this_plot = first_plot, i = 0;
  2099.          i < curve && this_plot != NULL;
  2100.          i++, this_plot = this_plot->next_cp);
  2101.     if (this_plot == NULL)
  2102.         printf("Curve %d does not exist; list has %d curves\n", curve, i);
  2103.     else {
  2104.         printf("Curve %d, %d points\n", curve, this_plot->p_count);
  2105.         for (i = 0; i < this_plot->p_count; i++) {
  2106.         printf("%c x=%g y=%g z=%g ylow=%g yhigh=%g\n",
  2107.                this_plot->points[i].type == INRANGE ? 'i'
  2108.                : this_plot->points[i].type == OUTRANGE ? 'o'
  2109.                : 'u',
  2110.                this_plot->points[i].x,
  2111.                this_plot->points[i].y,
  2112.                this_plot->points[i].z,
  2113.                this_plot->points[i].ylow,
  2114.                this_plot->points[i].yhigh);
  2115.         }
  2116.         printf("\n");
  2117.     }
  2118.     }
  2119. }
  2120. #endif
  2121.  
  2122. #ifdef THINK_C_2
  2123. print_table()
  2124. {
  2125.     register struct curve_points *this_plot;
  2126.     int             i, curve;
  2127.  
  2128.     for (this_plot = first_plot, curve = 0; this_plot != NULL;
  2129.      curve++, this_plot = this_plot->next_cp) {
  2130.     fprintf(outfile, "Curve %d, %d points\n", curve, this_plot->p_count);
  2131.     for (i = 0; i < this_plot->p_count; i++) {
  2132.         fprintf(outfile, "%c x=%g y=%g\n",
  2133.             this_plot->points[i].type == INRANGE ? 'i'
  2134.             : this_plot->points[i].type == OUTRANGE ? 'o'
  2135.             : 'u',
  2136.             this_plot->points[i].x,
  2137.             this_plot->points[i].y);
  2138.     }
  2139.     fprintf(outfile, "\n");
  2140.     }
  2141.     fflush(outfile);
  2142. }
  2143.  
  2144. print_3dtable(pcount)
  2145. int pcount;
  2146. {
  2147.     register struct surface_points *this_plot;
  2148.     int             i, curve,surface;
  2149.     struct iso_curve *icrvs;
  2150.     struct coordinate GPHUGE *points;
  2151.  
  2152.     for (surface=0, this_plot=first_3dplot ; surface < pcount; 
  2153.         this_plot=this_plot->next_sp, surface++){
  2154.         fprintf(outfile, "\nSurface %d of %d surfaces\n", surface, pcount);
  2155.         icrvs = this_plot->iso_crvs;
  2156.         curve = 0;
  2157.  
  2158.         while(icrvs){
  2159.             fprintf(outfile, "\nIsoCurve %d, %d points\n", curve, icrvs->p_count);
  2160.             for(i=0, points = icrvs->points; i < icrvs->p_count; i++){
  2161.                 fprintf(outfile, "%c x=%g y=%g z=%g\n",
  2162.                 points[i].type == INRANGE ? 'i'
  2163.                 : points[i].type == OUTRANGE ? 'o'
  2164.                 : 'u',
  2165.                 points[i].x,
  2166.                 points[i].y,
  2167.                 points[i].z);
  2168.             }
  2169.             icrvs = icrvs->next;
  2170.             curve++;
  2171.         }
  2172.     fprintf(outfile, "\n");
  2173.     }
  2174.     fflush(outfile);
  2175. }
  2176.  
  2177. /*
  2178.  * This parses the plot command after any range specifications. To support
  2179.  * autoscaling on the x axis, we want any data files to define the x range,
  2180.  * then to plot any functions using that range. We thus parse the input
  2181.  * twice, once to pick up the data files, and again to pick up the functions.
  2182.  * Definitions are processed twice, but that won't hurt.
  2183.  */
  2184. eval_plots()
  2185. {
  2186.     register int    i;
  2187.     register struct curve_points *this_plot, **tp_ptr;
  2188.     register int    start_token, end_token;
  2189.     register int    begin_token;
  2190.     double          x_min, x_max, y_min, y_max;
  2191.     register double x, xdiff, temp;
  2192.     static struct value a;
  2193.     TBOOLEAN         ltmp, some_data_files = FALSE,is_log_func = FALSE;
  2194.     int             plot_num, line_num, point_num, xparam = 0;
  2195.     char           *xtitle;
  2196.     void            parametric_fixup();
  2197.  
  2198.     /* Reset first_plot. This is usually done at the end of this function.
  2199.        If there is an error within this function, the memory is left allocated,
  2200.        since we cannot call cp_free if the list is incomplete. Making sure that
  2201.        the list structure is always vaild requires some rewriting */
  2202.     first_plot=NULL;
  2203.  
  2204.     if (autoscale_ly) {
  2205.     ymin = VERYLARGE;
  2206.     ymax = -VERYLARGE;
  2207.     } else if (is_log_y && (ymin <= 0.0 || ymax <= 0.0))
  2208.     int_error("y range must be above 0 for log scale!",
  2209.           NO_CARET);
  2210.  
  2211.     tp_ptr = &(first_plot);
  2212.     plot_num = 0;
  2213.     line_num = 0;        /* default line type */
  2214.     point_num = 0;        /* default point type */
  2215.  
  2216.     xtitle = NULL;
  2217.  
  2218.     begin_token = c_token;
  2219.  
  2220.     /*** First Pass: Read through data files ***
  2221.      * This pass serves to set the xrange and to parse the command, as well
  2222.      * as filling in every thing except the function data. That is done after
  2223.      * the xrange is defined.
  2224.      */
  2225.     while (TRUE) {
  2226.     if (END_OF_COMMAND)
  2227.         int_error("function to plot expected", c_token);
  2228.  
  2229.     start_token = c_token;
  2230.  
  2231.     if (is_definition(c_token)) {
  2232.         define();
  2233.     } else {
  2234.         plot_num++;
  2235.  
  2236.         if (isstring(c_token)) {    /* data file to plot */
  2237.         if (parametric && xparam)
  2238.             int_error("previous parametric function not fully specified",
  2239.                   c_token);
  2240.  
  2241.         if (!some_data_files && autoscale_lx) {
  2242.             xmin = VERYLARGE;
  2243.             xmax = -VERYLARGE;
  2244.         }
  2245.         some_data_files = TRUE;
  2246.  
  2247.         if (*tp_ptr)
  2248.             this_plot = *tp_ptr;
  2249.         else {        /* no memory malloc()'d there yet */
  2250.             this_plot = cp_alloc(MIN_CRV_POINTS);
  2251.             *tp_ptr = this_plot;
  2252.         }
  2253.         this_plot->plot_type = DATA;
  2254.         this_plot->plot_style = data_style;
  2255.         end_token = c_token;
  2256.         get_data(this_plot);    /* this also parses the using option */
  2257.         } else {        /* function to plot */
  2258.         if (parametric)    /* working on x parametric function */
  2259.             xparam = 1 - xparam;
  2260.         if (*tp_ptr) {
  2261.             this_plot = *tp_ptr;
  2262.             cp_extend(this_plot, samples + 1);
  2263.         } else {    /* no memory malloc()'d there yet */
  2264.             this_plot = cp_alloc(samples + 1);
  2265.             *tp_ptr = this_plot;
  2266.         }
  2267.         this_plot->plot_type = FUNC;
  2268.         this_plot->plot_style = func_style;
  2269.         dummy_func = &plot_func;
  2270.         plot_func.at = temp_at();
  2271.         /* ignore it for now */
  2272.         end_token = c_token - 1;
  2273.         }
  2274.  
  2275.         if (almost_equals(c_token, "t$itle")) {
  2276.         if (parametric) {
  2277.             if (xparam)
  2278.             int_error(
  2279.                      "\"title\" allowed only after parametric function fully specified",
  2280.                      c_token);
  2281.             else if (xtitle != NULL)
  2282.             xtitle[0] = '\0';    /* Remove default title . */
  2283.         }
  2284.         c_token++;
  2285.         if (isstring(c_token)) {
  2286.             m_quote_capture(&(this_plot->title), c_token, c_token);
  2287.         } else {
  2288.             int_error("expecting \"title\" for plot", c_token);
  2289.         }
  2290.         c_token++;
  2291.         } else if (almost_equals(c_token, "not$itle")) {
  2292.             c_token++;
  2293.         } else {
  2294.         m_capture(&(this_plot->title), start_token, end_token);
  2295.         if (xparam)
  2296.             xtitle = this_plot->title;
  2297.         }
  2298.  
  2299.         this_plot->line_type = line_num;
  2300.         this_plot->point_type = point_num;
  2301.  
  2302.         if (almost_equals(c_token, "w$ith")) {
  2303.         if (parametric && xparam)
  2304.             int_error("\"with\" allowed only after parametric function fully specified",
  2305.                   c_token);
  2306.         this_plot->plot_style = get_style();
  2307.         }
  2308.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2309.         struct value    t;
  2310.         this_plot->line_type = (int) real(const_express(&t)) - 1;
  2311.         }
  2312.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2313.         struct value    t;
  2314.         this_plot->point_type = (int) real(const_express(&t)) - 1;
  2315.         }
  2316.         if ((this_plot->plot_style == POINTSTYLE) ||
  2317.         (this_plot->plot_style == LINESPOINTS) ||
  2318.         (this_plot->plot_style == ERRORBARS))
  2319.         if (!xparam)
  2320.             point_num++;
  2321.         if (!xparam)
  2322.         line_num++;
  2323.  
  2324.         if (this_plot->plot_type == DATA)
  2325.         /* now that we know the plot style, adjust the yrange */
  2326.         adjust_yrange(this_plot);
  2327.  
  2328.         tp_ptr = &(this_plot->next_cp);
  2329.     }
  2330.  
  2331.     if (equals(c_token, ","))
  2332.         c_token++;
  2333.     else
  2334.         break;
  2335.     }
  2336.  
  2337.     if (parametric && xparam)
  2338.     int_error("parametric function not fully specified", NO_CARET);
  2339.  
  2340.     if (parametric) {
  2341.     /* Swap t and x ranges for duration of these eval_plot computations. */
  2342.     ltmp = autoscale_lx;
  2343.     autoscale_lx = autoscale_lt;
  2344.     autoscale_lt = ltmp;
  2345.     temp = xmin;
  2346.     xmin = tmin;
  2347.     tmin = temp;
  2348.     temp = xmax;
  2349.     xmax = tmax;
  2350.     tmax = temp;
  2351.     }
  2352.     /*** Second Pass: Evaluate the functions ***/
  2353.     /*
  2354.      * Everything is defined now, except the function data. We expect no
  2355.      * syntax errors, etc, since the above parsed it all. This makes the code
  2356.      * below simpler. If autoscale_ly, the yrange may still change.
  2357.      */
  2358.     if (fabs(xmax - xmin) < zero)
  2359.     if (autoscale_lx) {
  2360.         fprintf(stderr, "Warning: empty %c range [%g:%g], ",
  2361.             parametric ? 't' : 'x', xmin, xmax);
  2362.         if (fabs(xmin) < zero) {
  2363.         /* completely arbitary */
  2364.         xmin = -1.;
  2365.         xmax = 1.;
  2366.         } else {
  2367.         /* expand range by 10% in either direction */
  2368.         xmin = xmin * 0.9;
  2369.         xmax = xmax * 1.1;
  2370.         }
  2371.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2372.     } else {
  2373.         int_error("x range is less than `zero`", c_token);
  2374.     }
  2375.  
  2376.     /* give error if xrange badly set from missing datafile error */
  2377.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2378.     int_error("x range is invalid", c_token);
  2379.     }
  2380.     if (is_log_x) {
  2381.     if (xmin <= 0.0 || xmax <= 0.0)
  2382.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2383.     x_min = log(xmin)/log_base_log_x;
  2384.     x_max = log(xmax)/log_base_log_x;
  2385.     } else {
  2386.     x_min = xmin;
  2387.     x_max = xmax;
  2388.     }
  2389.  
  2390.     xdiff = (x_max - x_min) / (samples - 1);
  2391.  
  2392.     tp_ptr = &(first_plot);
  2393.     plot_num = 0;
  2394.     this_plot = first_plot;
  2395.     c_token = begin_token;    /* start over */
  2396.  
  2397.     /* Read through functions */
  2398.     while (TRUE) {
  2399.     if (is_definition(c_token)) {
  2400.         define();
  2401.     } else {
  2402.         plot_num++;
  2403.         if (isstring(c_token)) {    /* data file to plot */
  2404.         /* ignore this now */
  2405.         c_token++;
  2406.  
  2407.         /*
  2408.          * jev -- support for passing data from file thru user
  2409.          * function
  2410.          */
  2411.         if (almost_equals(c_token, "thru$")) {
  2412.             struct udft_entry tmp;
  2413.             c_token++;
  2414.             dummy_func = &tmp;
  2415.             (void) temp_at();
  2416.         }
  2417.         if (almost_equals(c_token, "u$sing")) {
  2418.             c_token++;    /* skip "using" */
  2419.             if (!isstring(c_token)) {
  2420.             struct value    a;
  2421.             (void) magnitude(const_express(&a));    /* skip xcol */
  2422.             if (equals(c_token, ":")) {
  2423.                 c_token++;    /* skip ":" */
  2424.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2425.             }
  2426.             if (equals(c_token, ":")) {
  2427.                 c_token++;    /* skip ":" */
  2428.                 (void) magnitude(const_express(&a));    /* skip yemin */
  2429.             }
  2430.             if (equals(c_token, ":")) {
  2431.                 c_token++;    /* skip ":" */
  2432.                 (void) magnitude(const_express(&a));    /* skip yemax */
  2433.             }
  2434.             if (equals(c_token, ":")) {
  2435.                 c_token++;    /* skip ":" */
  2436.                 (void) magnitude(const_express(&a));    /* skip wcol */
  2437.             }
  2438.             }
  2439.             if (isstring(c_token))
  2440.             c_token++;    /* skip format string */
  2441.         }
  2442.         } else {        /* function to plot */
  2443.         if (parametric)    /* working on x parametric function */
  2444.             xparam = 1 - xparam;
  2445.         dummy_func = &plot_func;
  2446.         plot_func.at = temp_at();    /* reparse function */
  2447.  
  2448.         is_log_func=parametric?(xparam?is_log_x:is_log_y):is_log_y;
  2449.         for (i = 0; i < samples; i++) {
  2450.             x = x_min + i * xdiff;
  2451.             /* if (is_log_x) PEM fix logscale x axis */
  2452.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2453.             (void) Gcomplex(&plot_func.dummy_values[0],
  2454.                    is_log_x ? pow(base_log_x, x) : x,
  2455.                    0.0);
  2456.  
  2457.             evaluate_at(plot_func.at, &a);
  2458.  
  2459.             if (undefined || (fabs(imag(&a)) > zero)) {
  2460.             this_plot->points[i].type = UNDEFINED;
  2461.             continue;
  2462.             }
  2463.             temp = real(&a);
  2464.             
  2465.             if (is_log_func && temp < 0.0) {
  2466.             this_plot->points[i].type = UNDEFINED;
  2467.             continue;
  2468.             }
  2469.             this_plot->points[i].x = x;
  2470.             this_plot->points[i].z = -1.0;  /* width of box not specified */
  2471.  
  2472.             if (is_log_func) {
  2473.             if (temp == 0.0) {
  2474.                 this_plot->points[i].type = OUTRANGE;
  2475.                 this_plot->points[i].y = -VERYLARGE;
  2476.                 continue;
  2477.             } else {
  2478.                 this_plot->points[i].y = log(temp)/log_base_log_y;
  2479.             }
  2480.             } else
  2481.             this_plot->points[i].y = temp;
  2482.  
  2483.             if (autoscale_ly || polar
  2484.             || inrange(temp, ymin, ymax)) {
  2485.             this_plot->points[i].type = INRANGE;
  2486.             /* When xparam is 1 we are not really computing y's! */
  2487.             if (!xparam && autoscale_ly) {
  2488.                 if (temp < ymin)
  2489.                 ymin = temp;
  2490.                 if (temp > ymax)
  2491.                 ymax = temp;
  2492.             }
  2493.             } else
  2494.             this_plot->points[i].type = OUTRANGE;
  2495.         }
  2496.         this_plot->p_count = i;    /* samples */
  2497.         }
  2498.  
  2499.         /* title was handled above */
  2500.         if (almost_equals(c_token, "t$itle")) {
  2501.         c_token++;
  2502.         c_token++;
  2503.         } else if (almost_equals(c_token, "not$itle")) {
  2504.             c_token++;
  2505.         }
  2506.         /* style was handled above */
  2507.         if (almost_equals(c_token, "w$ith")) {
  2508.         c_token++;
  2509.         c_token++;
  2510.         }
  2511.         /* line and point types were handled above */
  2512.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2513.         struct value    t;
  2514.         (void) real(const_express(&t));
  2515.         }
  2516.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2517.         struct value    t;
  2518.         (void) real(const_express(&t));
  2519.         }
  2520.         tp_ptr = &(this_plot->next_cp);    /* used below */
  2521.         this_plot = this_plot->next_cp;
  2522.     }
  2523.  
  2524.     if (equals(c_token, ","))
  2525.         c_token++;
  2526.     else
  2527.         break;
  2528.     }
  2529.  
  2530.     /* throw out all curve_points at end of list, that we don't need  */
  2531.     cp_free(*tp_ptr);
  2532.     *tp_ptr = NULL;
  2533.  
  2534.     /* if first_plot is NULL, we have no functions or data at all. This can
  2535.        happen, if you type "plot x=5", since x=5 is a variable assignment */
  2536.  
  2537.     if(first_plot==NULL) {
  2538.     int_error("no functions or data to plot", c_token);
  2539.     }
  2540.  
  2541.     if (fabs(ymax - ymin) < zero)
  2542.     /* if autoscale, widen range */
  2543.     if (autoscale_ly) {
  2544.         fprintf(stderr, "Warning: empty y range [%g:%g], ", ymin, ymax);
  2545.         if (fabs(ymin) < zero) {
  2546.         ymin = -1.;
  2547.         ymax = 1.;
  2548.         } else {
  2549.         /* expand range by 10% in either direction */
  2550.         ymin = ymin * 0.9;
  2551.         ymax = ymax * 1.1;
  2552.         }
  2553.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2554.     } else {
  2555.         int_error("y range is less than `zero`", c_token);
  2556.     }
  2557.  
  2558.     /* Now we finally know the real ymin and ymax */
  2559.     if (is_log_y) {
  2560.     y_min = log(ymin)/log_base_log_y;
  2561.     y_max = log(ymax)/log_base_log_y;
  2562.     } else {
  2563.     y_min = ymin;
  2564.     y_max = ymax;
  2565.     }
  2566.  
  2567.     /* Set a flag so capture is not invoked  by replot itself. -hmh */
  2568.     if (plot_token != -1) {
  2569.         capture(replot_line, plot_token, c_token);
  2570.         plot_token = -1;        
  2571.     }
  2572.   
  2573.     if (parametric) {
  2574.     /* Now put t and x ranges back before we actually plot anything. */
  2575.     ltmp = autoscale_lx;
  2576.     autoscale_lx = autoscale_lt;
  2577.     autoscale_lt = ltmp;
  2578.     temp = xmin;
  2579.     xmin = tmin;
  2580.     tmin = temp;
  2581.     temp = xmax;
  2582.     xmax = tmax;
  2583.     tmax = temp;
  2584.     if (some_data_files && autoscale_lx) {
  2585.         /*
  2586.          * Stop any further autoscaling in this case (may be a mistake,
  2587.          * have to consider what is really wanted some day in the
  2588.          * future--jdc).
  2589.          */
  2590.         autoscale_lx = 0;
  2591.     }
  2592.     /* Now actually fix the plot pairs to be single plots. */
  2593.     parametric_fixup(first_plot, &plot_num, &x_min, &x_max);
  2594.     }
  2595.     if (strcmp(term_tbl[term].name, "table") == 0)
  2596.     print_table();
  2597.     else
  2598.     do_plot(first_plot, plot_num, x_min, x_max, y_min, y_max);
  2599.     cp_free(first_plot);
  2600.     first_plot = NULL;
  2601. }
  2602.  
  2603. static void 
  2604. parse_title(crnt_param, start_token, end_token,
  2605.         xtitle, ytitle, this_plot, do_parse)
  2606. int crnt_param, start_token, end_token;
  2607. char **xtitle, **ytitle;
  2608. struct surface_points *this_plot;
  2609. TBOOLEAN do_parse;
  2610. {
  2611.     static char title[256];
  2612.  
  2613.     if (do_parse) {
  2614.     if (almost_equals(c_token, "t$itle")) {
  2615.         if (parametric) {
  2616.         if (crnt_param)
  2617.             int_error("\"title\" allowed only after parametric function fully specified",
  2618.                   c_token);
  2619.         else {
  2620.             /* Remove default title */
  2621.             if (*xtitle != NULL)
  2622.             (*xtitle)[0] = '\0';
  2623.             if (*ytitle != NULL)
  2624.             (*ytitle)[0] = '\0';
  2625.         }
  2626.         }
  2627.         c_token++;
  2628.         if (isstring(c_token)) {
  2629.         m_quote_capture(&(this_plot->title), c_token, c_token);
  2630.         } else {
  2631.         int_error("expecting \"title\" for plot", c_token);
  2632.         }
  2633.         c_token++;
  2634.     }  else if (almost_equals(c_token, "not$itle")) {
  2635.         c_token++;
  2636.     }  else {
  2637.         m_capture(&(this_plot->title), start_token, end_token);
  2638.         if (crnt_param == 1)
  2639.         *xtitle = this_plot->title;
  2640.         if (crnt_param == 2)
  2641.         *ytitle = this_plot->title;
  2642.     }
  2643.     }
  2644.     else {
  2645. #ifdef THINK_C
  2646.     this_plot->title = alloc(strlen(title) + 1,"string");
  2647. #else
  2648.     this_plot->title = alloc(strlen(title) + 1);
  2649. #endif
  2650.     strcpy(this_plot->title, title);
  2651.     }
  2652. }
  2653.  
  2654. /*
  2655.  * This parses the splot command after any range specifications. To support
  2656.  * autoscaling on the x/z axis, we want any data files to define the x/y
  2657.  * range, then to plot any functions using that range. We thus parse the
  2658.  * input twice, once to pick up the data files, and again to pick up the
  2659.  * functions. Definitions are processed twice, but that won't hurt.
  2660.  */
  2661. eval_3dplots()
  2662. {
  2663.     register int    i, j;
  2664.     register struct surface_points *this_plot=NULL, **tp_3d_ptr;
  2665.     register int    start_token, end_token;
  2666.     register int    begin_token;
  2667.     double          x_min, x_max, y_min, y_max, z_min, z_max;
  2668.     register double x, xdiff, xisodiff, y, ydiff, yisodiff, temp;
  2669.     static struct value a;
  2670.     TBOOLEAN         ltmp, some_data_files = FALSE,is_log_func = FALSE;
  2671.     int             plot_num, line_num, point_num, crnt_param = 0;    /* 0=z, 1=x, 2=y */
  2672.     char           *xtitle;
  2673.     char           *ytitle;
  2674.     void            parametric_3dfixup();
  2675.  
  2676.     /* Reset first_3dplot. This is usually done at the end of this function.
  2677.        If there is an error within this function, the memory is left allocated,
  2678.        since we cannot call sp_free if the list is incomplete */
  2679.     first_3dplot=NULL;
  2680.  
  2681.     if (autoscale_lz) {
  2682.     zmin = VERYLARGE;
  2683.     zmax = -VERYLARGE;
  2684.     } else if (is_log_z && (zmin <= 0.0 || zmax <= 0.0))
  2685.     int_error("z range must be above 0 for log scale!",
  2686.           NO_CARET);
  2687.  
  2688.     tp_3d_ptr = &(first_3dplot);
  2689.     plot_num = 0;
  2690.     line_num = 0;        /* default line type */
  2691.     point_num = 0;        /* default point type */
  2692.  
  2693.     xtitle = NULL;
  2694.     ytitle = NULL;
  2695.  
  2696.     begin_token = c_token;
  2697.  
  2698.     /*** First Pass: Read through data files ***/
  2699.     /*
  2700.      * This pass serves to set the x/yranges and to parse the command, as
  2701.      * well as filling in every thing except the function data. That is done
  2702.      * after the x/yrange is defined.
  2703.      */
  2704.     while (TRUE) {
  2705.     if (END_OF_COMMAND)
  2706.         int_error("function to plt3d expected", c_token);
  2707.  
  2708.     start_token = c_token;
  2709.  
  2710.     if (is_definition(c_token)) {
  2711.         define();
  2712.     } else {
  2713.         plot_num++;
  2714.  
  2715.         if (isstring(c_token)) {    /* data file to plot */
  2716.         int line_type = line_num,
  2717.             point_type = point_num,
  2718.             plot_style = data_style,
  2719.             first_mesh = TRUE;
  2720.  
  2721.         if (parametric && crnt_param != 0)
  2722.             int_error("previous parametric function not fully specified",
  2723.                   c_token);
  2724.  
  2725.         if (!some_data_files) {
  2726.             if (autoscale_lx) {
  2727.             xmin = VERYLARGE;
  2728.             xmax = -VERYLARGE;
  2729.             }
  2730.             if (autoscale_ly) {
  2731.             ymin = VERYLARGE;
  2732.             ymax = -VERYLARGE;
  2733.             }
  2734.         }
  2735.         some_data_files = TRUE;
  2736.  
  2737.         do {
  2738.             if (*tp_3d_ptr)
  2739.             this_plot = *tp_3d_ptr;
  2740.             else {        /* no memory malloc()'d there yet */
  2741.             /* Allocate enough isosamples and samples */
  2742.             this_plot = sp_alloc(0, 0, 0, 0);
  2743.             *tp_3d_ptr = this_plot;
  2744.             }
  2745.  
  2746.             this_plot->plot_type = DATA3D;
  2747.             end_token = c_token;
  2748.             /* this also parses index/using option */
  2749.             get_3ddata(this_plot);
  2750.  
  2751.             parse_title(crnt_param, start_token, end_token,
  2752.                 &xtitle, &ytitle, this_plot, first_mesh);
  2753.             if (!first_mesh) plot_num++;
  2754.  
  2755.             if (first_mesh) {
  2756.             if (almost_equals(c_token, "w$ith")) {
  2757.                 plot_style = this_plot->plot_style = get_style();
  2758.             }
  2759.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2760.                 struct value    t;
  2761.                 line_type = (int) real(const_express(&t)) - 1;
  2762.             }
  2763.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2764.                 struct value    t;
  2765.                 point_type = (int) real(const_express(&t)) - 1;
  2766.             }
  2767.             first_mesh = FALSE;
  2768.             }
  2769.  
  2770.             this_plot->line_type = line_type;
  2771.             this_plot->point_type = point_type;
  2772.             this_plot->plot_style = plot_style;
  2773.  
  2774.             tp_3d_ptr = &(this_plot->next_sp);
  2775.         }
  2776.         while (more_data_fp);
  2777.         } else {        /* function to plot */
  2778.         if (parametric)    /* Rotate between x/y/z axes */
  2779.             crnt_param = (crnt_param + 1) % 3;
  2780.         if (*tp_3d_ptr) {
  2781.             this_plot = *tp_3d_ptr;
  2782.             if (!hidden3d)
  2783.             sp_replace(this_plot, samples_1, iso_samples_1,
  2784.                                    samples_2, iso_samples_2);
  2785.             else
  2786.             sp_replace(this_plot, iso_samples_1, 0,
  2787.                                    0, iso_samples_2);
  2788.         } else {    /* no memory malloc()'d there yet */
  2789.             /* Allocate enough isosamples and samples */
  2790.             if (!hidden3d)
  2791.             this_plot = sp_alloc(samples_1, iso_samples_1,
  2792.                                              samples_2, iso_samples_2);
  2793.             else
  2794.             this_plot = sp_alloc(iso_samples_1, 0,
  2795.                                              0, iso_samples_2);
  2796.             *tp_3d_ptr = this_plot;
  2797.         }
  2798.  
  2799.         this_plot->plot_type = FUNC3D;
  2800.         this_plot->has_grid_topology = TRUE;
  2801.         this_plot->plot_style = func_style;
  2802.         dummy_func = &plot_func;
  2803.         plot_func.at = temp_at();
  2804.         /* ignore it for now */
  2805.         end_token = c_token - 1;
  2806.  
  2807.         parse_title(crnt_param, start_token, end_token,
  2808.                 &xtitle, &ytitle, this_plot, TRUE);
  2809.  
  2810.         this_plot->line_type = line_num;
  2811.         this_plot->point_type = point_num;
  2812.  
  2813.         if (almost_equals(c_token, "w$ith")) {
  2814.             this_plot->plot_style = get_style();
  2815.         }
  2816.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2817.             struct value    t;
  2818.             this_plot->line_type = (int) real(const_express(&t)) - 1;
  2819.         }
  2820.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2821.             struct value    t;
  2822.             this_plot->point_type = (int) real(const_express(&t)) - 1;
  2823.         }
  2824.  
  2825.         tp_3d_ptr = &(this_plot->next_sp);
  2826.         }
  2827.  
  2828.         if ((this_plot->plot_style == POINTSTYLE) ||
  2829.         (this_plot->plot_style == LINESPOINTS) ||
  2830.         (this_plot->plot_style == ERRORBARS))
  2831.         if (crnt_param == 0)
  2832.             point_num +=
  2833.             1 + (draw_contour != 0)
  2834.             + (hidden3d != 0);
  2835.         if (crnt_param == 0)
  2836.         line_num += 1 + (draw_contour != 0)
  2837.             + (hidden3d != 0);
  2838.     }
  2839.  
  2840.     if (equals(c_token, ","))
  2841.         c_token++;
  2842.     else
  2843.         break;
  2844.     }
  2845.  
  2846.     if (parametric && crnt_param != 0)
  2847.     int_error("parametric function not fully specified", NO_CARET);
  2848.  
  2849.     if (parametric) {
  2850.     /*
  2851.      * Swap u/v and x/y ranges for duration of these eval_plot
  2852.      * computations.
  2853.      */
  2854.     ltmp = autoscale_lx;
  2855.     autoscale_lx = autoscale_lu;
  2856.     autoscale_lu = ltmp;
  2857.     ltmp = autoscale_ly;
  2858.     autoscale_ly = autoscale_lv;
  2859.     autoscale_lv = ltmp;
  2860.     temp = xmin;
  2861.     xmin = umin;
  2862.     umin = temp;
  2863.     temp = xmax;
  2864.     xmax = umax;
  2865.     umax = temp;
  2866.     temp = ymin;
  2867.     ymin = vmin;
  2868.     vmin = temp;
  2869.     temp = ymax;
  2870.     ymax = vmax;
  2871.     vmax = temp;
  2872.     }
  2873.     /*** Second Pass: Evaluate the functions ***/
  2874.     /*
  2875.      * Everything is defined now, except the function data. We expect no
  2876.      * syntax errors, etc, since the above parsed it all. This makes the code
  2877.      * below simpler. If autoscale_ly, the yrange may still change.
  2878.      */
  2879.     if (xmin == xmax)
  2880.     if (autoscale_lx) {
  2881.         fprintf(stderr, "Warning: empty x range [%g:%g], ",
  2882.             xmin, xmax);
  2883.         if (xmin == 0.0) {
  2884.         /* completely arbitary */
  2885.         xmin = -1.;
  2886.         xmax = 1.;
  2887.         } else {
  2888.         /* expand range by 10% in either direction */
  2889.         xmin = xmin * 0.9;
  2890.         xmax = xmax * 1.1;
  2891.         }
  2892.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2893.     } else {
  2894.         int_error("x range is empty", c_token);
  2895.     }
  2896.  
  2897.     if (ymin == ymax)
  2898.     if (autoscale_ly) {
  2899.         fprintf(stderr, "Warning: empty y range [%g:%g], ",
  2900.             ymin, ymax);
  2901.         if (ymin == 0.0) {
  2902.         /* completely arbitary */
  2903.         ymin = -1.;
  2904.         ymax = 1.;
  2905.         } else {
  2906.         /* expand range by 10% in either direction */
  2907.         ymin = ymin * 0.9;
  2908.         ymax = ymax * 1.1;
  2909.         }
  2910.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2911.     } else {
  2912.         int_error("y range is empty", c_token);
  2913.     }
  2914.  
  2915.     /* give error if xrange badly set from missing datafile error */
  2916.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2917.     int_error("x range is invalid", c_token);
  2918.     }
  2919.     if (is_log_x) {
  2920.     if (xmin <= 0.0 || xmax <= 0.0)
  2921.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2922.     x_min = log(xmin)/log_base_log_x;
  2923.     x_max = log(xmax)/log_base_log_x;
  2924.     } else {
  2925.     x_min = xmin;
  2926.     x_max = xmax;
  2927.     }
  2928.  
  2929.     /* give error if yrange badly set from previous error */
  2930.     if (ymin == VERYLARGE || ymax == -VERYLARGE) {
  2931.     int_error("y range is invalid", c_token);
  2932.     }
  2933.     if (is_log_y) {
  2934.     if (ymin <= 0.0 || ymax <= 0.0)
  2935.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  2936.     y_min = log(ymin)/log_base_log_y;
  2937.     y_max = log(ymax)/log_base_log_y;
  2938.     } else {
  2939.     y_min = ymin;
  2940.     y_max = ymax;
  2941.     }
  2942.  
  2943.     if (samples_1 < 2 || samples_2 < 2 || iso_samples_1 < 2 || iso_samples_2 < 2)
  2944.     int_error("samples or iso_samples < 2. Must be at least 2.", NO_CARET);
  2945.  
  2946.     if (this_plot && this_plot->has_grid_topology && hidden3d) {
  2947.     xdiff = (x_max - x_min) / (iso_samples_1 - 1);
  2948.     ydiff = (y_max - y_min) / (iso_samples_2 - 1);
  2949.     } else {
  2950.     xdiff = (x_max - x_min) / (samples_1 - 1);
  2951.     ydiff = (y_max - y_min) / (samples_2 - 1);
  2952.     }
  2953.     xisodiff = (x_max - x_min) / (iso_samples_1 - 1);
  2954.     yisodiff = (y_max - y_min) / (iso_samples_2 - 1);
  2955.  
  2956.     this_plot = first_3dplot;
  2957.     c_token = begin_token;    /* start over */
  2958.  
  2959.     /* Read through functions */
  2960.     while (TRUE) {
  2961.     if (is_definition(c_token)) {
  2962.         define();
  2963.     } else {
  2964.         if (isstring(c_token)) {    /* data file to plot */
  2965.         /* ignore this now */
  2966.         c_token++;
  2967.         if (almost_equals(c_token, "i$ndex")) {
  2968.             struct value a;
  2969.             int index;
  2970.  
  2971.             c_token++;        /* skip "index" */
  2972.             index = (int) magnitude(const_express(&a));
  2973.         }
  2974.         if (almost_equals(c_token, "u$sing")) {
  2975.             c_token++;    /* skip "using" */
  2976.             if (!isstring(c_token)) {
  2977.             struct value    a;
  2978.             (void) magnitude(const_express(&a));    /* skip xcol */
  2979.             if (equals(c_token, ":")) {
  2980.                 c_token++;    /* skip ":" */
  2981.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2982.                 if (equals(c_token, ":")) {
  2983.                 c_token++;    /* skip ":" */
  2984.                 (void) magnitude(const_express(&a));    /* skip zcol */
  2985.                 }
  2986.             }
  2987.             }
  2988.             if (isstring(c_token))
  2989.             c_token++;    /* skip format string */
  2990.         }
  2991.         } else {        /* function to plot */
  2992.         struct iso_curve *this_iso = this_plot->iso_crvs;
  2993.         struct coordinate GPHUGE *points = this_iso->points;
  2994.         int num_sam_to_use, num_iso_to_use;
  2995.  
  2996.         if (parametric)
  2997.             crnt_param = (crnt_param + 1) % 3;
  2998.         dummy_func = &plot_func;
  2999.         plot_func.at = temp_at();    /* reparse function */
  3000.  
  3001.         num_iso_to_use = iso_samples_2;
  3002.         if (!(this_plot->has_grid_topology && hidden3d))
  3003.             num_sam_to_use = samples_1;
  3004.         else
  3005.             num_sam_to_use = iso_samples_1;
  3006.  
  3007.         is_log_func=(!parametric)||(crnt_param==0)?is_log_z:(crnt_param==1?is_log_x:is_log_y);
  3008.         for (j = 0; j < num_iso_to_use; j++) {
  3009.             y = y_min + j * yisodiff;
  3010.             /* if (is_log_y) PEM fix logscale y axis */
  3011.             /* y = pow(log_base_log_y,y); 26-Sep-89 */
  3012.             (void) Gcomplex(&plot_func.dummy_values[1],
  3013.                    is_log_y ? pow(base_log_y, y) : y,
  3014.                    0.0);
  3015.  
  3016.             for (i = 0; i < num_sam_to_use; i++) {
  3017.             x = x_min + i * xdiff;
  3018.             /* if (is_log_x) PEM fix logscale x axis */
  3019.             /* x = pow(base_log_x,x); 26-Sep-89 */
  3020.             (void) Gcomplex(&plot_func.dummy_values[0],
  3021.                        is_log_x ? pow(base_log_x, x) : x,
  3022.                        0.0);
  3023.  
  3024.             points[i].x = x;
  3025.             points[i].y = y;
  3026.  
  3027.             evaluate_at(plot_func.at, &a);
  3028.  
  3029.             if (undefined || (fabs(imag(&a)) > zero)) {
  3030.                 points[i].type = UNDEFINED;
  3031.                 continue;
  3032.             }
  3033.             temp = real(&a);
  3034.  
  3035.             if (is_log_func && temp < 0.0) {
  3036.                 points[i].type = UNDEFINED;
  3037.                 continue;
  3038.             }
  3039.             if (is_log_func) {
  3040.                 if (temp == 0.0) {
  3041.                 points[i].type = OUTRANGE;
  3042.                 points[i].z = -VERYLARGE;
  3043.                 continue;
  3044.                 } else {
  3045.                 points[i].z = log(temp)/log_base_log_z;
  3046.                 }
  3047.             } else
  3048.                 points[i].z = temp;
  3049.  
  3050.             if (autoscale_lz || inrange(temp, zmin, zmax)) {
  3051.                 points[i].type = INRANGE;
  3052.                 if (autoscale_lz) {
  3053.                 if (temp < zmin)
  3054.                     zmin = temp;
  3055.                 if (temp > zmax)
  3056.                     zmax = temp;
  3057.                 }
  3058.             } else
  3059.                 points[i].type = OUTRANGE;
  3060.             }
  3061.             this_iso->p_count = num_sam_to_use;
  3062.             this_iso = this_iso->next;
  3063.             points = this_iso? this_iso->points: NULL;
  3064.         }
  3065.  
  3066.         if (!(this_plot->has_grid_topology && hidden3d)) {
  3067.             num_iso_to_use = iso_samples_1;
  3068.             num_sam_to_use = samples_2;
  3069.             is_log_func=(!parametric)||(crnt_param==0)?is_log_z:(crnt_param==1?is_log_x:is_log_y);
  3070.             for (i = 0; i < num_iso_to_use; i++) {
  3071.             x = x_min + i * xisodiff;
  3072.             /* if (is_log_x) PEM fix logscale x axis */
  3073.             /* x = pow(base_log_x,x); 26-Sep-89 */
  3074.             (void) Gcomplex(&plot_func.dummy_values[0],
  3075.                        is_log_x ? pow(base_log_x, x) : x,
  3076.                        0.0);
  3077.  
  3078.             for (j = 0; j < num_sam_to_use; j++) {
  3079.                 y = y_min + j * ydiff;
  3080.                 /* if (is_log_y) PEM fix logscale y axis */
  3081.                 /* y = pow(base_log_y,y); 26-Sep-89 */
  3082.                 (void) Gcomplex(&plot_func.dummy_values[1],
  3083.                        is_log_y ? pow(base_log_y, y) : y,
  3084.                        0.0);
  3085.  
  3086.                 points[j].x = x;
  3087.                 points[j].y = y;
  3088.  
  3089.                 evaluate_at(plot_func.at, &a);
  3090.  
  3091.                 if (undefined || (fabs(imag(&a)) > zero)) {
  3092.                 points[j].type = UNDEFINED;
  3093.                 continue;
  3094.                 }
  3095.                 temp = real(&a);
  3096.  
  3097.                 if (is_log_func && temp < 0.0) {
  3098.                 points[j].type = UNDEFINED;
  3099.                 continue;
  3100.                 }
  3101.                 if (is_log_func) {
  3102.                 if (temp == 0.0) {
  3103.                     points[j].type = OUTRANGE;
  3104.                     points[j].z = -VERYLARGE;
  3105.                     continue;
  3106.                 } else {
  3107.                     points[j].z = log(temp)/log_base_log_z;
  3108.                 }
  3109.                 } else
  3110.                 points[j].z = temp;
  3111.  
  3112.                 if (autoscale_lz
  3113.                 || inrange(temp, zmin, zmax)) {
  3114.                 points[j].type = INRANGE;
  3115.                 if (autoscale_lz) {
  3116.                     if (temp < zmin)
  3117.                     zmin = temp;
  3118.                     if (temp > zmax)
  3119.                     zmax = temp;
  3120.                 }
  3121.                 } else
  3122.                 points[j].type = OUTRANGE;
  3123.             }
  3124.             this_iso->p_count = num_sam_to_use;
  3125.             this_iso = this_iso->next;
  3126.             points = this_iso ? this_iso->points : NULL;
  3127.             }
  3128.         }
  3129.         }
  3130.  
  3131.         /* title was handled above */
  3132.         if (almost_equals(c_token, "t$itle")) {
  3133.         c_token++;
  3134.         c_token++;
  3135.         } else if (almost_equals(c_token, "not$itle")) {
  3136.             c_token++;
  3137.         }
  3138.         /* style was handled above */
  3139.         if (almost_equals(c_token, "w$ith")) {
  3140.         c_token++;
  3141.         c_token++;
  3142.         }
  3143.         /* line and point types were handled above */
  3144.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  3145.         struct value    t;
  3146.         (void) real(const_express(&t));
  3147.         }
  3148.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  3149.         struct value    t;
  3150.         (void) real(const_express(&t));
  3151.         }
  3152.         this_plot = this_plot->next_sp;
  3153.     }
  3154.  
  3155.     if (equals(c_token, ","))
  3156.         c_token++;
  3157.     else
  3158.         break;
  3159.     }
  3160.  
  3161.     /* if first_3dplot is NULL, we have no functions or data at all. This can
  3162.        happen, if you type "splot x=5", since x=5 is a variable assignment */
  3163.  
  3164.     if(first_3dplot==NULL) {
  3165.     int_error("no functions or data to plot", c_token);
  3166.     }
  3167.  
  3168.     if (fabs(zmax - zmin) < zero)
  3169.     /* if autoscale, widen range */
  3170.     if (autoscale_lz) {
  3171.         fprintf(stderr, "Warning: empty z range [%g:%g], ", zmin, zmax);
  3172.         if (fabs(zmin) < zero) {
  3173.         zmin = -1.;
  3174.         zmax = 1.;
  3175.         } else {
  3176.         /* expand range by 10% in either direction */
  3177.         zmin = zmin * 0.9;
  3178.         zmax = zmax * 1.1;
  3179.         }
  3180.         fprintf(stderr, "adjusting to [%g:%g]\n", zmin, zmax);
  3181.     } else {
  3182.         int_error("z range is less than `zero`", c_token);
  3183.     }
  3184.  
  3185.     /* Now we finally know the real zmin and zmax */
  3186.     if (is_log_z) {
  3187.     if (zmin <= 0.0 || zmax <= 0.0)
  3188.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  3189.     z_min = log(zmin)/log_base_log_z;
  3190.     z_max = log(zmax)/log_base_log_z;
  3191.     } else {
  3192.     z_min = zmin;
  3193.     z_max = zmax;
  3194.     }
  3195.  
  3196.     /* Set a flag so capture is not invoked by replot itself. -hmh */
  3197.     if (plot_token != -1) {
  3198.     capture(replot_line, plot_token, c_token);
  3199.     plot_token = -1;
  3200.     }
  3201.  
  3202.     if (parametric) {
  3203.     /* Now put u/v and x/y ranges back before we actually plot anything. */
  3204.     ltmp = autoscale_lx;
  3205.     autoscale_lx = autoscale_lu;
  3206.     autoscale_lu = ltmp;
  3207.     ltmp = autoscale_ly;
  3208.     autoscale_ly = autoscale_lv;
  3209.     autoscale_lv = ltmp;
  3210.     temp = xmin;
  3211.     xmin = umin;
  3212.     umin = temp;
  3213.     temp = xmax;
  3214.     xmax = umax;
  3215.     umax = temp;
  3216.     temp = ymin;
  3217.     ymin = vmin;
  3218.     vmin = temp;
  3219.     temp = ymax;
  3220.     ymax = vmax;
  3221.     vmax = temp;
  3222.  
  3223.     /* Now actually fix the plot triplets to be single plots. */
  3224.     parametric_3dfixup(first_3dplot, &plot_num,
  3225.                &x_min, &x_max, &y_min, &y_max,
  3226.                &z_min, &z_max);
  3227.     if (is_log_x) {
  3228.         if (x_min <= 0.0 || x_max <= 0.0)
  3229.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  3230.         x_min = log(x_min)/log_base_log_x;
  3231.         x_max = log(x_max)/log_base_log_x;
  3232.     }
  3233.     if (is_log_y) {
  3234.         if (y_min <= 0.0 || y_max <= 0.0)
  3235.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  3236.         y_min = log(y_min)/log_base_log_y;
  3237.         y_max = log(y_max)/log_base_log_y;
  3238.     }
  3239.     if (is_log_z) {
  3240.         if (z_min <= 0.0 || z_max <= 0.0)
  3241.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  3242.         z_min = log(z_min)/log_base_log_z;
  3243.         z_max = log(z_max)/log_base_log_z;
  3244.     }
  3245.     }
  3246.  
  3247.     /* Filter out empty meshes. */
  3248.     while (first_3dplot &&
  3249.        first_3dplot->num_iso_read == 0 &&
  3250.        first_3dplot->plot_type == DATA3D) {
  3251.     struct surface_points *plt = first_3dplot->next_sp;
  3252.  
  3253.     first_3dplot->next_sp = NULL;
  3254.     sp_free(first_3dplot);
  3255.     plot_num--;
  3256.     first_3dplot = plt;
  3257.     }
  3258.     if (first_3dplot != NULL) {
  3259.     struct surface_points *plt1, *plt2;
  3260.     
  3261.     for (plt1 = first_3dplot, plt2 = plt1->next_sp; plt2 != NULL; ) {
  3262.         if (plt2->num_iso_read == 0 && plt2->plot_type == DATA3D) {
  3263.         plt2 = plt2->next_sp;
  3264.         plt1->next_sp->next_sp = NULL;
  3265.         sp_free(plt1->next_sp);
  3266.         plot_num--;
  3267.         plt1->next_sp = plt2;
  3268.         }
  3269.         else {
  3270.         plt1 = plt2;
  3271.         plt2 = plt2->next_sp;
  3272.         }
  3273.     }
  3274.     }
  3275.     if (first_3dplot == NULL)
  3276.     int_error("no data found in file", NO_CARET);
  3277.  
  3278.     /* Creates contours if contours are to be plotted as well. */
  3279.     if (draw_contour) {
  3280.     for (this_plot = first_3dplot, i = 0;
  3281.          i < plot_num;
  3282.          this_plot = this_plot->next_sp, i++) {
  3283.         if (this_plot->contours) {
  3284.         struct gnuplot_contours *cntr, *cntrs = this_plot->contours;
  3285.  
  3286.         while (cntrs) {
  3287.             cntr = cntrs;
  3288.             cntrs = cntrs->next;
  3289. #ifdef THINK_C
  3290.             gpfarfree((char GPFAR *)cntr->coords);
  3291. #else
  3292.             gpfarfree(cntr->coords);
  3293. #endif
  3294.             free(cntr);
  3295.         }
  3296.         }
  3297.         /* Make sure this one can be contoured. */
  3298.         if (!this_plot->has_grid_topology) {
  3299.         this_plot->contours = NULL;
  3300.         fprintf(stderr,"Notice: cannot contour non grid data!\n");
  3301.         /* changed from int_error by recommendation of rkc@xn.ll.mit.edu */
  3302.         }
  3303.         else if (this_plot->plot_type == DATA3D)
  3304.         this_plot->contours = contour(
  3305.                          this_plot->num_iso_read,
  3306.                          this_plot->iso_crvs,
  3307.                          contour_levels, contour_pts,
  3308.                          contour_kind, contour_order,
  3309.                          levels_kind, levels_list);
  3310.         else
  3311.         this_plot->contours = contour(iso_samples_2,
  3312.                           this_plot->iso_crvs,
  3313.                           contour_levels, contour_pts,
  3314.                           contour_kind, contour_order,
  3315.                           levels_kind, levels_list);
  3316.     }
  3317.     }
  3318.     if (strcmp(term_tbl[term].name, "table") == 0)
  3319.     print_3dtable(plot_num);
  3320.     else
  3321.     do_3dplot(first_3dplot, plot_num, x_min, x_max, y_min, y_max, z_min, z_max);
  3322.     sp_free(first_3dplot);
  3323.     first_3dplot = NULL;
  3324. }
  3325.  
  3326. done(status)
  3327.     int             status;
  3328. {
  3329.     if (term && term_init)
  3330.     (*term_tbl[term].reset) ();
  3331. #ifdef vms
  3332.     vms_reset();
  3333. #endif
  3334.     exit(status);
  3335. }
  3336.  
  3337. void 
  3338. parametric_fixup(start_plot, plot_num, x_min, x_max)
  3339.     struct curve_points *start_plot;
  3340.     int            *plot_num;
  3341.     double         *x_min, *x_max;
  3342. /*
  3343.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3344.  * list (which are gauranteed to occur in (x,y) pairs while preserving the
  3345.  * non-FUNC type plots intact.  This means we have to work our way through
  3346.  * various lists.  Examples (hand checked): start_plot:F1->F2->NULL ==>
  3347.  * F2->NULL start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F2->F4->F6->NULL
  3348.  * start_plot:F1->F2->D1->D2->F3->F4->D3->NULL ==> F2->D1->D2->F4->D3->NULL
  3349.  * 
  3350.  * Of course, the more interesting work is to move the y values of the x
  3351.  * function to become the x values of the y function (checking the mins and
  3352.  * maxs as we go along).
  3353.  */
  3354. {
  3355.     struct curve_points *xp, *new_list, *yp = start_plot, *tmp, *free_list,
  3356.                    *free_head = NULL;
  3357.     int             i, tlen, curve;
  3358.     char           *new_title;
  3359.     double          lxmin, lxmax, temp;
  3360.  
  3361.     if (autoscale_lx) {
  3362.     lxmin = VERYLARGE;
  3363.     lxmax = -VERYLARGE;
  3364.     } else {
  3365.     lxmin = xmin;
  3366.     lxmax = xmax;
  3367.     }
  3368.  
  3369.     /*
  3370.      * Ok, go through all the plots and move FUNC types together.  Note: this
  3371.      * originally was written to look for a NULL next pointer, but gnuplot
  3372.      * wants to be sticky in grabbing memory and the right number of items in
  3373.      * the plot list is controlled by the plot_num variable.
  3374.      * 
  3375.      * Since gnuplot wants to do this sticky business, a free_list of
  3376.      * curve_points is kept and then tagged onto the end of the plot list as
  3377.      * this seems more in the spirit of the original memory behavior than
  3378.      * simply freeing the memory.  I'm personally not convinced this sort of
  3379.      * concern is worth it since the time spent computing points seems to
  3380.      * dominate any garbage collecting that might be saved here...
  3381.      */
  3382.     new_list = xp = start_plot;
  3383.     yp = xp->next_cp;
  3384.     curve = 0;
  3385.  
  3386.     for (; curve < *plot_num; xp = xp->next_cp, yp = yp ? yp->next_cp : yp, curve++) {
  3387.     if (xp->plot_type != FUNC) {
  3388.         continue;
  3389.     }
  3390.     /* Here's a FUNC parametric function defined as two parts. */
  3391.     --(*plot_num);
  3392.     /*
  3393.      * Go through all the points assigning the y's from xp to be the x's
  3394.      * for yp.  Check max's and min's as you go.
  3395.      */
  3396.     for (i = 0; i < yp->p_count; ++i) {
  3397.         /*
  3398.          * Throw away excess xp points, mark excess yp points as
  3399.          * OUTRANGE.
  3400.          */
  3401.         if (i > xp->p_count) {
  3402.         yp->points[i].type = OUTRANGE;
  3403.         continue;
  3404.         }
  3405.         /*
  3406.          * Just as we had to do when we computed y values--now check that
  3407.          * x's (computed parametrically) are in the permitted ranges as
  3408.          * well.
  3409.          */
  3410.         temp = xp->points[i].y;    /* New x value for yp function. */
  3411.         yp->points[i].x = temp;
  3412.         /* Handle undefined values differently from normal ranges. */
  3413.         if (xp->points[i].type == UNDEFINED)
  3414.         yp->points[i].type = xp->points[i].type;
  3415.         if (autoscale_lx || polar
  3416.         || inrange(temp, lxmin, lxmax)) {
  3417.         if (autoscale_lx && temp < lxmin)
  3418.             lxmin = temp;
  3419.         if (autoscale_lx && temp > lxmax)
  3420.             lxmax = temp;
  3421.         } else
  3422.         yp->points[i].type = OUTRANGE;    /* Due to x value. */
  3423.     }
  3424.     /* Ok, fix up the title to include both the xp and yp plots. */
  3425.     if (xp->title && xp->title[0] != '\0') {
  3426.         tlen = strlen(yp->title) + strlen(xp->title) + 3;
  3427.         new_title = alloc((unsigned long) tlen, "string");
  3428.         strcpy(new_title, xp->title);
  3429.         strcat(new_title, ", ");    /* + 2 */
  3430.         strcat(new_title, yp->title);    /* + 1 = + 3 */
  3431.         free(yp->title);
  3432.         yp->title = new_title;
  3433.     }
  3434.     /* Eliminate the first curve (xparam) and just use the second. */
  3435.     if (xp == start_plot) {
  3436.         /* Simply nip off the first element of the list. */
  3437.         new_list = first_plot = yp;
  3438.         xp = xp->next_cp;
  3439.         if (yp->next_cp != NULL)
  3440.         yp = yp->next_cp;
  3441.         /* Add start_plot to the free_list. */
  3442.         if (free_head == NULL) {
  3443.         free_list = free_head = start_plot;
  3444.         free_head->next_cp = NULL;
  3445.         } else {
  3446.         free_list->next_cp = start_plot;
  3447.         start_plot->next_cp = NULL;
  3448.         free_list = start_plot;
  3449.         }
  3450.     } else {
  3451.         /* Here, remove the xp node and replace it with the yp node. */
  3452.         tmp = xp;
  3453.         /* Pass over any data files that might have been in place. */
  3454.         while (new_list->next_cp && new_list->next_cp != xp)
  3455.         new_list = new_list->next_cp;
  3456.         new_list->next_cp = yp;
  3457.         new_list = new_list->next_cp;
  3458.         xp = xp->next_cp;
  3459.         if (yp->next_cp != NULL)
  3460.         yp = yp->next_cp;
  3461.         /* Add tmp to the free_list. */
  3462.         tmp->next_cp = NULL;
  3463.         if (free_head == NULL) {
  3464.         free_list = free_head = tmp;
  3465.         } else {
  3466.         free_list->next_cp = tmp;
  3467.         free_list = tmp;
  3468.         }
  3469.     }
  3470.     }
  3471.     /* Ok, stick the free list at the end of the curve_points plot list. */
  3472.     while (new_list->next_cp != NULL)
  3473.     new_list = new_list->next_cp;
  3474.     new_list->next_cp = free_head;
  3475.  
  3476.     /* Report the overall graph mins and maxs. */
  3477.     *x_min = lxmin;
  3478.     *x_max = lxmax;
  3479. }
  3480.  
  3481. void 
  3482. parametric_3dfixup(start_plot, plot_num, x_min, x_max, y_min, y_max,
  3483.            z_min, z_max)
  3484.     struct surface_points *start_plot;
  3485.     int            *plot_num;
  3486.     double         *x_min, *x_max, *y_min, *y_max, *z_min, *z_max;
  3487. /*
  3488.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3489.  * list (which are gauranteed to occur in (x,y,z) triplets while preserving
  3490.  * the non-FUNC type plots intact.  This means we have to work our way
  3491.  * through various lists.  Examples (hand checked):
  3492.  * start_plot:F1->F2->F3->NULL ==> F3->NULL
  3493.  * start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F3->F6->NULL
  3494.  * start_plot:F1->F2->F3->D1->D2->F4->F5->F6->D3->NULL ==>
  3495.  * F3->D1->D2->F6->D3->NULL
  3496.  */
  3497. {
  3498.     struct surface_points *xp, *yp, *zp, *new_list, *tmp, *free_list, *free_head = NULL;
  3499.     struct iso_curve *icrvs, *xicrvs, *yicrvs, *zicrvs;
  3500.     int             i, tlen, surface;
  3501.     char           *new_title;
  3502.     double          lxmin, lxmax, lymin, lymax, lzmin, lzmax;
  3503.  
  3504.     if (autoscale_lx) {
  3505.     lxmin = VERYLARGE;
  3506.     lxmax = -VERYLARGE;
  3507.     } else {
  3508.     lxmin = xmin;
  3509.     lxmax = xmax;
  3510.     }
  3511.  
  3512.     if (autoscale_ly) {
  3513.     lymin = VERYLARGE;
  3514.     lymax = -VERYLARGE;
  3515.     } else {
  3516.     lymin = ymin;
  3517.     lymax = ymax;
  3518.     }
  3519.  
  3520.     if (autoscale_lz) {
  3521.     lzmin = VERYLARGE;
  3522.     lzmax = -VERYLARGE;
  3523.     } else {
  3524.     lzmin = zmin;
  3525.     lzmax = zmax;
  3526.     }
  3527.  
  3528.     /*
  3529.      * Ok, go through all the plots and move FUNC3D types together.  Note:
  3530.      * this originally was written to look for a NULL next pointer, but
  3531.      * gnuplot wants to be sticky in grabbing memory and the right number of
  3532.      * items in the plot list is controlled by the plot_num variable.
  3533.      * 
  3534.      * Since gnuplot wants to do this sticky business, a free_list of
  3535.      * surface_points is kept and then tagged onto the end of the plot list
  3536.      * as this seems more in the spirit of the original memory behavior than
  3537.      * simply freeing the memory.  I'm personally not convinced this sort of
  3538.      * concern is worth it since the time spent computing points seems to
  3539.      * dominate any garbage collecting that might be saved here...
  3540.      */
  3541.     new_list = xp = start_plot;
  3542.     for (surface = 0; surface < *plot_num; surface++) {
  3543.     if (xp->plot_type != FUNC3D) {
  3544.         icrvs = xp->iso_crvs;
  3545.  
  3546.         while (icrvs) {
  3547.         struct coordinate GPHUGE *points = icrvs->points;
  3548.  
  3549.         for (i = 0; i < icrvs->p_count; ++i) {
  3550.             if (lxmin > points[i].x)
  3551.             lxmin = points[i].x;
  3552.             if (lxmax < points[i].x)
  3553.             lxmax = points[i].x;
  3554.             if (lymin > points[i].y)
  3555.             lymin = points[i].y;
  3556.             if (lymax < points[i].y)
  3557.             lymax = points[i].y;
  3558.             if (lzmin > points[i].z)
  3559.             lzmin = points[i].z;
  3560.             if (lzmax < points[i].z)
  3561.             lzmax = points[i].z;
  3562.         }
  3563.  
  3564.         icrvs = icrvs->next;
  3565.         }
  3566.         xp = xp->next_sp;
  3567.         continue;
  3568.     }
  3569.     yp = xp->next_sp;
  3570.     zp = yp->next_sp;
  3571.  
  3572.     /* Here's a FUNC3D parametric function defined as three parts. */
  3573.     (*plot_num) -= 2;
  3574.     /*
  3575.      * Go through all the points and assign the x's and y's from xp and
  3576.      * yp to zp.  Check max's and min's as you go.
  3577.      */
  3578.     xicrvs = xp->iso_crvs;
  3579.     yicrvs = yp->iso_crvs;
  3580.     zicrvs = zp->iso_crvs;
  3581.     while (zicrvs) {
  3582.         struct coordinate GPHUGE *xpoints = xicrvs->points, GPHUGE *ypoints = yicrvs->points, GPHUGE *zpoints = zicrvs->points;
  3583.         for (i = 0; i < zicrvs->p_count; ++i) {
  3584.         zpoints[i].x = xpoints[i].z;
  3585.         zpoints[i].y = ypoints[i].z;
  3586.  
  3587.         if (lxmin > zpoints[i].x)
  3588.             lxmin = zpoints[i].x;
  3589.         if (lxmax < zpoints[i].x)
  3590.             lxmax = zpoints[i].x;
  3591.         if (lymin > zpoints[i].y)
  3592.             lymin = zpoints[i].y;
  3593.         if (lymax < zpoints[i].y)
  3594.             lymax = zpoints[i].y;
  3595.         if (lzmin > zpoints[i].z)
  3596.             lzmin = zpoints[i].z;
  3597.         if (lzmax < zpoints[i].z)
  3598.             lzmax = zpoints[i].z;
  3599.         }
  3600.         xicrvs = xicrvs->next;
  3601.         yicrvs = yicrvs->next;
  3602.         zicrvs = zicrvs->next;
  3603.     }
  3604.  
  3605.     /* Ok, fix up the title to include xp and yp plots. */
  3606.     if ((xp->title && xp->title[0] != '\0') ||
  3607.         (yp->title && yp->title[0] != '\0')) {
  3608.         tlen = (xp->title ? strlen(xp->title) : 0) +
  3609.         (yp->title ? strlen(yp->title) : 0) +
  3610.         (zp->title ? strlen(zp->title) : 0) + 5;
  3611.         new_title = alloc((unsigned long) tlen, "string");
  3612.         new_title[0] = 0;
  3613.         if (xp->title) {
  3614.         strcat(new_title, xp->title);
  3615.         strcat(new_title, ", ");    /* + 2 */
  3616.         }
  3617.         if (yp->title) {
  3618.         strcat(new_title, yp->title);
  3619.         strcat(new_title, ", ");    /* + 2 */
  3620.         }
  3621.         if (zp->title) {
  3622.         strcat(new_title, zp->title);
  3623.         }
  3624.         free(zp->title);
  3625.         zp->title = new_title;
  3626.     }
  3627.     /*
  3628.      * Eliminate the first two surfaces (xp and yp) and just use the
  3629.      * third.
  3630.      */
  3631.     if (xp == start_plot) {
  3632.         /* Simply nip off the first two elements of the list. */
  3633.         new_list = first_3dplot = zp;
  3634.         xp = zp->next_sp;
  3635.         /* Add xp and yp to the free_list. */
  3636.         if (free_head == NULL) {
  3637.         free_head = start_plot;
  3638.         } else {
  3639.         free_list->next_sp = start_plot;
  3640.         }
  3641.         free_list = start_plot->next_sp;
  3642.         free_list->next_sp = NULL;
  3643.     } else {
  3644.         /*
  3645.          * Here, remove the xp,yp nodes and replace them with the zp
  3646.          * node.
  3647.          */
  3648.         tmp = xp;
  3649.         /* Pass over any data files that might have been in place. */
  3650.         while (new_list->next_sp && new_list->next_sp != xp)
  3651.         new_list = new_list->next_sp;
  3652.         new_list->next_sp = zp;
  3653.         new_list = zp;
  3654.         xp = zp->next_sp;
  3655.         /* Add tmp to the free_list. */
  3656.         if (free_head == NULL) {
  3657.         free_head = tmp;
  3658.         } else {
  3659.         free_list->next_sp = tmp;
  3660.         }
  3661.         free_list = tmp->next_sp;
  3662.         free_list->next_sp = NULL;
  3663.     }
  3664.     }
  3665.     /* Ok, stick the free list at the end of the surface_points plot list. */
  3666.     while (new_list->next_sp != NULL)
  3667.     new_list = new_list->next_sp;
  3668.     new_list->next_sp = free_head;
  3669.     if (lxmax - lxmin < zero) {
  3670.     if (fabs(lxmax) < zero) {
  3671.         lxmin = -1.0;
  3672.         lxmax = 1.0;
  3673.     } else {
  3674.         lxmin *= 0.9;
  3675.         lxmax *= 1.1;
  3676.     }
  3677.     }
  3678.     if (lymax - lymin < zero) {
  3679.     if (fabs(lymax) < zero) {
  3680.         lymin = -1.0;
  3681.         lymax = 1.0;
  3682.     } else {
  3683.         lymin *= 0.9;
  3684.         lymax *= 1.1;
  3685.     }
  3686.     }
  3687.     if (lzmax - lzmin < zero) {
  3688.     if (fabs(lzmax) < zero) {
  3689.         lzmin = -1.0;
  3690.         lzmax = 1.0;
  3691.     } else {
  3692.         lzmin *= 0.9;
  3693.         lzmax *= 1.1;
  3694.     }
  3695.     }
  3696.     /* Report the overall graph mins and maxs. */
  3697.     if (autoscale_lx) {
  3698.     *x_min = (is_log_x ? pow(base_log_x, lxmin) : lxmin);
  3699.     *x_max = (is_log_x ? pow(base_log_x, lxmax) : lxmax);
  3700.     } else {
  3701.     *x_min = xmin;
  3702.     *x_max = xmax;
  3703.     }
  3704.     if (autoscale_ly) {
  3705.     *y_min = (is_log_y ? pow(base_log_y, lymin) : lymin);
  3706.     *y_max = (is_log_y ? pow(base_log_y, lymax) : lymax);
  3707.     } else {
  3708.     *y_min = ymin;
  3709.     *y_max = ymax;
  3710.     }
  3711.     if (autoscale_lz) {
  3712.     *z_min = (is_log_z ? pow(base_log_z, lzmin) : lzmin);
  3713.     *z_max = (is_log_z ? pow(base_log_z, lzmax) : lzmax);
  3714.     } else {
  3715.     *z_min = zmin;
  3716.     *z_max = zmax;
  3717.     }
  3718. }
  3719.  
  3720. #ifdef AMIGA_SC_6_1
  3721. void 
  3722. sleep(delay)
  3723.     unsigned int    delay;
  3724. {
  3725.     Delay(50 * delay);
  3726. }
  3727. #endif
  3728.  
  3729. #ifdef AMIGA_AC_5
  3730. void 
  3731. sleep(delay)
  3732.     unsigned int    delay;
  3733. {
  3734.     unsigned long   time_is_up;
  3735.     time_is_up = time(NULL) + (unsigned long) delay;
  3736.     while (time(NULL) < time_is_up)
  3737.      /* wait */ ;
  3738. }
  3739. #endif
  3740.  
  3741. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  3742. #if (!defined(__TURBOC__) && !defined(__EMX__) && !defined(DJGPP)) || defined(_Windows) /* Turbo C already has sleep() */
  3743. #ifndef __ZTC__            /* ZTC already has usleep() */
  3744. /* kludge to provide sleep() for msc 5.1 */
  3745. void 
  3746. sleep(delay)
  3747.     unsigned int    delay;
  3748. {
  3749.     unsigned long   time_is_up;
  3750.     time_is_up = time(NULL) + (unsigned long) delay;
  3751.     while (time(NULL) < time_is_up)
  3752.      /* wait */ ;
  3753. }
  3754. #endif                /* not ZTC */
  3755. #endif                /* (!TURBOC && !__EMX__ && !DJGPP) or _Windows */
  3756. #endif                /* MSDOS || _Windows*/
  3757.  
  3758.  
  3759. /* Support for input, shell, and help for various systems */
  3760.  
  3761. #ifdef vms
  3762.  
  3763. #include <descrip.h>
  3764. #include <rmsdef.h>
  3765. #include <errno.h>
  3766. #include <smgdef.h>
  3767. #include <smgmsg.h>
  3768.  
  3769. extern          lib$get_input(), lib$put_output();
  3770. extern          smg$read_composed_line();
  3771.  
  3772. int             vms_len;
  3773.  
  3774. unsigned int    status[2] =
  3775. {1, 0};
  3776.  
  3777. static char     help[MAX_LINE_LEN + 1] = "gnuplot";
  3778.  
  3779. $DESCRIPTOR(prompt_desc, PROMPT);
  3780. $DESCRIPTOR(line_desc, input_line);
  3781.  
  3782. $DESCRIPTOR(help_desc, help);
  3783. $DESCRIPTOR(helpfile_desc, "GNUPLOT$HELP");
  3784.  
  3785.  
  3786. read_line(prompt)
  3787.     char           *prompt;
  3788. {
  3789.     int             more, start = 0;
  3790.     char            expand_prompt[40];
  3791.  
  3792.     prompt_desc.dsc$w_length = strlen(prompt);
  3793.     prompt_desc.dsc$a_pointer = prompt;
  3794.     (void) strcpy(expand_prompt, "_");
  3795.     (void) strncat(expand_prompt, prompt, 38);
  3796.     do {
  3797.     line_desc.dsc$w_length = MAX_LINE_LEN - start;
  3798.     line_desc.dsc$a_pointer = &input_line[start];
  3799.     switch (status[1] = smg$read_composed_line(&vms_vkid, 0, &line_desc, &prompt_desc, &vms_len)) {
  3800.     case SMG$_EOF:
  3801.         done(IO_SUCCESS);    /* ^Z isn't really an error */
  3802.         break;
  3803.     case RMS$_TNS:        /* didn't press return in time */
  3804.         vms_len--;        /* skip the last character */
  3805.         break;        /* and parse anyway */
  3806.     case RMS$_BES:        /* Bad Escape Sequence */
  3807.     case RMS$_PES:        /* Partial Escape Sequence */
  3808.         sys$putmsg(status);
  3809.         vms_len = 0;    /* ignore the line */
  3810.         break;
  3811.     case SS$_NORMAL:
  3812.         break;        /* everything's fine */
  3813.     default:
  3814.         done(status[1]);    /* give the error message */
  3815.     }
  3816.     start += vms_len;
  3817.     input_line[start] = '\0';
  3818.     inline_num++;
  3819.     if (input_line[start - 1] == '\\') {
  3820.         /* Allow for a continuation line. */
  3821.         prompt_desc.dsc$w_length = strlen(expand_prompt);
  3822.         prompt_desc.dsc$a_pointer = expand_prompt;
  3823.         more = 1;
  3824.         --start;
  3825.     } else {
  3826.         line_desc.dsc$w_length = strlen(input_line);
  3827.         line_desc.dsc$a_pointer = input_line;
  3828.         more = 0;
  3829.     }
  3830.     } while (more);
  3831.     return 0;
  3832. }
  3833.  
  3834.  
  3835. do_help()
  3836. {
  3837.     help_desc.dsc$w_length = strlen(help);
  3838.     if ((vaxc$errno = lbr$output_help(lib$put_output, 0, &help_desc,
  3839.                &helpfile_desc, 0, lib$get_input)) != SS$_NORMAL)
  3840.     os_error("can't open GNUPLOT$HELP", NO_CARET);
  3841. }
  3842.  
  3843.  
  3844. do_shell()
  3845. {
  3846. #ifdef THINK_C
  3847.         int_error("'shell' is not supported on this version.",c_token);
  3848. #else
  3849.     if ((vaxc$errno = lib$spawn()) != SS$_NORMAL) {
  3850.     os_error("spawn error", NO_CARET);
  3851.     }
  3852. #endif
  3853. }
  3854.  
  3855.  
  3856. do_system()
  3857. {
  3858.     input_line[0] = ' ';    /* an embarrassment, but... */
  3859.  
  3860.     if ((vaxc$errno = lib$spawn(&line_desc)) != SS$_NORMAL)
  3861.     os_error("spawn error", NO_CARET);
  3862.  
  3863.     (void) putc('\n', stderr);
  3864. }
  3865.  
  3866. #else                /* vms */
  3867.  
  3868. #ifdef _Windows
  3869. do_help()
  3870. {
  3871.     if (END_OF_COMMAND)
  3872.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_INDEX,(DWORD)NULL);
  3873.     else {
  3874.         char buf[128];
  3875.         int start = c_token++;
  3876.         while (!(END_OF_COMMAND))
  3877.             c_token++;
  3878.         capture(buf, start, c_token-1);
  3879.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_PARTIALKEY,(DWORD)buf);
  3880.     }
  3881. }
  3882. #else
  3883.  
  3884. /*
  3885.  * do_help: (not VMS, although it would work) Give help to the user. It
  3886.  * parses the command line into helpbuf and supplies help for that string.
  3887.  * Then, if there are subtopics available for that key, it prompts the user
  3888.  * with this string. If more input is given, do_help is called recursively,
  3889.  * with the argument the index of null character in the string. Thus a more
  3890.  * specific help can be supplied. This can be done repeatedly. If null input
  3891.  * is given, the function returns, effecting a backward climb up the tree.
  3892.  * David Kotz (David.Kotz@Dartmouth.edu) 10/89
  3893.  */
  3894.  
  3895. do_help()
  3896. {
  3897.     static char    *helpbuf = NULL;
  3898.     static char    *prompt = NULL;
  3899.     int             base;    /* index of first char AFTER help string */
  3900.     int             len;    /* length of current help string */
  3901.     TBOOLEAN         more_help;
  3902.     TBOOLEAN         only;    /* TRUE if only printing subtopics */
  3903.     int             subtopics;    /* 0 if no subtopics for this topic */
  3904.     int             start;    /* starting token of help string */
  3905.     char           *help_ptr;    /* name of help file */
  3906. #ifdef ATARI
  3907.     static char    help_fname[256]=""; /* keep helpfilename across calls */
  3908. #endif
  3909.  
  3910.     if ((help_ptr = getenv("GNUHELP")) == (char *) NULL)
  3911. #ifndef ATARI
  3912.     /* if can't find environment variable then just use HELPFILE */
  3913.     help_ptr = HELPFILE;
  3914. #else
  3915.     /* try whether we can find the helpfile via shell_find. If not, just
  3916.        use the default. (tnx Andreas) */
  3917.  
  3918. #ifdef sequent
  3919.     if( !index( HELPFILE, ':' ) && !index( HELPFILE, '/' ) &&
  3920.         !index( HELPFILE, '\\' ) ) {
  3921. #else
  3922.     if( !strchr( HELPFILE, ':' ) && !strchr( HELPFILE, '/' ) &&
  3923.         !strchr( HELPFILE, '\\' ) ) {
  3924. #endif
  3925.         if( strlen(help_fname)==0 ) {
  3926.         strcpy( help_fname, HELPFILE );
  3927.         if( shel_find( help_fname )==0 ) {
  3928.             strcpy( help_fname, HELPFILE );
  3929.         }
  3930.         }
  3931.         help_ptr=help_fname;
  3932.     } else {
  3933.         help_ptr=HELPFILE;
  3934.     }
  3935. #endif /* ATARI */
  3936.  
  3937.     /* Since MSDOS DGROUP segment is being overflowed we can not allow such  */
  3938.     /* huge static variables (1k each). Instead we dynamically allocate them */
  3939.     /* on the first call to this function...                     */
  3940.     if (helpbuf == NULL) {
  3941. #ifdef THINK_C
  3942.     helpbuf = alloc((size_t)MAX_LINE_LEN, "help buffer");
  3943.     prompt = alloc((size_t)MAX_LINE_LEN, "help prompt");
  3944. #else
  3945.     helpbuf = alloc((unsigned long)MAX_LINE_LEN, "help buffer");
  3946.     prompt = alloc((unsigned long)MAX_LINE_LEN, "help prompt");
  3947. #endif
  3948.     helpbuf[0] = prompt[0] = 0;
  3949.     }
  3950.     len = base = strlen(helpbuf);
  3951.  
  3952.     /* find the end of the help command */
  3953.     for (start = c_token; !(END_OF_COMMAND); c_token++);
  3954.     /* copy new help input into helpbuf */
  3955.     if (len > 0)
  3956.     helpbuf[len++] = ' ';    /* add a space */
  3957.     capture(helpbuf + len, start, c_token - 1);
  3958.     squash_spaces(helpbuf + base);    /* only bother with new stuff */
  3959.     lower_case(helpbuf + base);    /* only bother with new stuff */
  3960.     len = strlen(helpbuf);
  3961.  
  3962.     /* now, a lone ? will print subtopics only */
  3963.     if (strcmp(helpbuf + (base ? base + 1 : 0), "?") == 0) {
  3964.     /* subtopics only */
  3965.     subtopics = 1;
  3966.     only = TRUE;
  3967.     helpbuf[base] = '\0';    /* cut off question mark */
  3968.     } else {
  3969.     /* normal help request */
  3970.     subtopics = 0;
  3971.     only = FALSE;
  3972.     }
  3973.  
  3974.     switch (help(helpbuf, help_ptr, &subtopics)) {
  3975.     case H_FOUND:{
  3976.         /* already printed the help info */
  3977.         /* subtopics now is true if there were any subtopics */
  3978.         screen_ok = FALSE;
  3979.  
  3980.         do {
  3981.         if (subtopics && !only) {
  3982.             /* prompt for subtopic with current help string */
  3983.             if (len > 0)
  3984.             (void) sprintf(prompt, "Subtopic of %s: ", helpbuf);
  3985.             else
  3986.             (void) strcpy(prompt, "Help topic: ");
  3987.             read_line(prompt);
  3988.             num_tokens = scanner(input_line);
  3989.             c_token = 0;
  3990.             more_help = !(END_OF_COMMAND);
  3991.             if (more_help)
  3992.             /* base for next level is all of current helpbuf */
  3993.             do_help();
  3994.         } else
  3995.             more_help = FALSE;
  3996.         } while (more_help);
  3997.  
  3998.         break;
  3999.     }
  4000.     case H_NOTFOUND:{
  4001.         printf("Sorry, no help for '%s'\n", helpbuf);
  4002.         break;
  4003.     }
  4004.     case H_ERROR:{
  4005.         perror(help_ptr);
  4006.         break;
  4007.     }
  4008.     default:{            /* defensive programming */
  4009.         int_error("Impossible case in switch", NO_CARET);
  4010.         /* NOTREACHED */
  4011.     }
  4012.     }
  4013.  
  4014.     helpbuf[base] = '\0';    /* cut it off where we started */
  4015. }
  4016. #endif  /* _Windows */
  4017.  
  4018. #ifdef AMIGA_AC_5
  4019. char            strg0[256];
  4020. #endif
  4021.  
  4022. do_system()
  4023. {
  4024. #ifdef AMIGA_AC_5
  4025.     char           *parms[80];
  4026.     void            getparms();
  4027.  
  4028.     getparms(input_line + 1, parms);
  4029.     if (fexecv(parms[0], parms) < 0)
  4030. #else
  4031. #if defined(ATARI)&&defined(__GNUC__)
  4032.     /* use preloaded shell, if available */
  4033.     short           (*shell_p) (char *command);
  4034.     void           *ssp;
  4035.  
  4036.     ssp = (void *) Super(NULL);
  4037.     shell_p = *(short (**) (char *)) 0x4f6;
  4038.     Super(ssp);
  4039.  
  4040.     /* this is a bit strange, but we have to have a single if */
  4041.     if ((shell_p ? (*shell_p) (input_line + 1) : system(input_line + 1)))
  4042. #else
  4043. #ifdef _Windows
  4044.     if (winsystem(input_line + 1))
  4045. #else
  4046.     if (system(input_line + 1))
  4047. #endif
  4048. #endif
  4049. #endif
  4050.     os_error("system() failed", NO_CARET);
  4051. }
  4052.  
  4053. #ifdef AMIGA_AC_5
  4054.  
  4055. /******************************************************************************/
  4056. /* */
  4057. /* Parses the command string (for fexecv use) and  converts the first token  */
  4058. /* to lower case                                                          */
  4059. /* */
  4060. /******************************************************************************/
  4061.  
  4062. void 
  4063. getparms(command, parms)
  4064.     char           *command;
  4065.     char          **parms;
  4066. {
  4067.     register int    i = 0;    /* A bunch of indices          */
  4068.     register int    j = 0;
  4069.     register int    k = 0;
  4070.  
  4071.     while (*(command + j) != '\0') {    /* Loop on string characters   */
  4072.     parms[k++] = strg0 + i;
  4073.     while (*(command + j) == ' ')
  4074.         ++j;
  4075.     while (*(command + j) != ' ' && *(command + j) != '\0') {
  4076.         if (*(command + j) == '"')    /* Get quoted string           */
  4077.         for (*(strg0 + (i++)) = *(command + (j++));
  4078.              *(command + j) != '"';
  4079.              *(strg0 + (i++)) = *(command + (j++)));
  4080.         *(strg0 + (i++)) = *(command + (j++));
  4081.     }
  4082.     *(strg0 + (i++)) = '\0';/* NUL terminate every token   */
  4083.     }
  4084.     parms[k] = '\0';
  4085.  
  4086.     for (k = strlen(strg0) - 1; k >= 0; --k)    /* Convert to lower case       */
  4087.     *(strg0 + k) >= 'A' && *(strg0 + k) <= 'Z' ? *(strg0 + k) |= 32 : *(strg0 + k);
  4088. }
  4089.  
  4090. #endif                /* AMIGA_AC_5 */
  4091.  
  4092. #ifdef READLINE
  4093. char           *
  4094. rlgets(s, n, prompt)
  4095.     char           *s;
  4096.     int             n;
  4097.     char           *prompt;
  4098. {
  4099.     char           *readline();
  4100.     static char    *line = (char *) NULL;
  4101.  
  4102.     /* If we already have a line, first free it */
  4103.     if (line != (char *) NULL)
  4104.     free(line);
  4105.  
  4106.     line = readline((interactive) ? prompt : "");
  4107.  
  4108.     /* If it's not an EOF */
  4109.     if (line) {
  4110.     if (*line)
  4111.         add_history(line);
  4112.     strncpy(s, line, n);
  4113.     return s;
  4114.     }
  4115.     return line;
  4116. }
  4117. #endif                /* READLINE */
  4118.  
  4119. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  4120.  
  4121. #ifndef _Windows
  4122. #ifdef __TURBOC__ 
  4123. /* cgets implemented using dos functions */
  4124. /* Maurice Castro 22/5/91 */
  4125. char           *
  4126. doscgets(s)
  4127.     char           *s;
  4128. {
  4129.     long            datseg;
  4130.  
  4131.     /* protect and preserve segments - call dos to do the dirty work */
  4132.     datseg = _DS;
  4133.  
  4134.     _DX = FP_OFF(s);
  4135.     _DS = FP_SEG(s);
  4136.     _AH = 0x0A;
  4137.     geninterrupt(33);
  4138.     _DS = datseg;
  4139.  
  4140.     /* check for a carriage return and then clobber it with a null */
  4141.     if (s[s[1] + 2] == '\r')
  4142.     s[s[1] + 2] = 0;
  4143.  
  4144.     /* return the input string */
  4145.     return (&(s[2]));
  4146. }
  4147. #endif                /* __TURBOC__ */
  4148. #endif                /* !_Windows */
  4149.  
  4150. #ifdef __ZTC__
  4151. void cputs(char *s)
  4152. {
  4153.    register int i = 0;
  4154.    while (s[i] != '\0')  bdos(0x02, s[i++], NULL);
  4155. }
  4156. char *cgets(char *s)
  4157. {
  4158.    bdosx(0x0A, s, NULL);
  4159.  
  4160.    if (s[s[1]+2] == '\r')
  4161.       s[s[1]+2] = 0;
  4162.  
  4163.    /* return the input string */
  4164.    return(&(s[2]));
  4165. }
  4166. #endif   /* __ZTC__ */
  4167.  
  4168.  
  4169. read_line(prompt)
  4170.     char           *prompt;
  4171. {
  4172.     register int    i;
  4173.     int             start = 0, ilen = 0;
  4174.     TBOOLEAN         more;
  4175.     int             last;
  4176.     char           *p, *crnt_prompt = prompt;
  4177.  
  4178.     if (interactive) {        /* if interactive use console IO so CED will
  4179.                  * work */
  4180. #ifndef READLINE
  4181. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  4182.     printf("%s", prompt);
  4183. #else
  4184.     cputs(prompt);
  4185. #endif
  4186. #endif                /* READLINE */
  4187.     do {
  4188.         ilen = MAX_LINE_LEN - start - 1;
  4189.         input_line[start] = ilen > 126 ? 126 : ilen;
  4190. #ifdef READLINE
  4191.         input_line[start + 2] = 0;
  4192.         (void) rlgets(&(input_line[start + 2]), ilen, crnt_prompt);
  4193. #ifdef sequent
  4194.         if ((p = index(&(input_line[start + 2]), '\r'))!=NULL)
  4195.         *p = 0;
  4196.         if ((p = index(&(input_line[start + 2]), '\n'))!=NULL)
  4197.         *p = 0;
  4198. #else
  4199.         if ((p = strchr(&(input_line[start + 2]), '\r'))!=NULL)
  4200.         *p = 0;
  4201.         if ((p = strchr(&(input_line[start + 2]), '\n'))!=NULL)
  4202.         *p = 0;
  4203. #endif
  4204.         input_line[start + 1] = strlen(&(input_line[start + 2]));
  4205. #else                /* READLINE */
  4206. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP)
  4207.         (void) gets(&(input_line[start+2]));
  4208. #else
  4209. #ifdef __TURBOC__
  4210.         (void) doscgets(&(input_line[start]));
  4211. #else                /* __TURBOC__ */
  4212.         (void) cgets(&(input_line[start]));
  4213. #endif                /* __TURBOC__ */
  4214. #endif                /* _Windows || __EMX__ || DJGPP*/
  4215.         (void) putc('\n', stderr);
  4216. #endif                /* READLINE */
  4217.         if (input_line[start + 2] == 26) {
  4218.         /* end-of-file */
  4219.         (void) putc('\n', stderr);
  4220.         input_line[start] = '\0';
  4221.         inline_num++;
  4222.         if (start > 0)    /* don't quit yet - process what we have */
  4223.             more = FALSE;
  4224.         else {
  4225.             (void) putc('\n', stderr);
  4226.             return(1); /* exit gnuplot */
  4227.             /* NOTREACHED */
  4228.         }
  4229.         } else {
  4230.         /* normal line input */
  4231.         register        i = start;
  4232.         while ((input_line[i] = input_line[i + 2]) != (char) NULL)
  4233.             i++;    /* yuck!  move everything down two characters */
  4234.  
  4235.         inline_num++;
  4236.         last = strlen(input_line) - 1;
  4237.         if (last<0) last=0;  /* stop UAE in Windows */
  4238.         if (last + 1 >= MAX_LINE_LEN)
  4239.             int_error("Input line too long", NO_CARET);
  4240.  
  4241.         if (input_line[last] == '\\') {    /* line continuation */
  4242.             start = last;
  4243.             more = TRUE;
  4244.         } else
  4245.             more = FALSE;
  4246.         }
  4247. #ifndef READLINE
  4248.         if (more)
  4249. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  4250.         printf("> ");
  4251. #else
  4252.         cputs("> ");
  4253. #endif
  4254. #else
  4255.         crnt_prompt = "> ";
  4256. #endif                /* READLINE */
  4257.     } while (more);
  4258.     } else {            /* not interactive */
  4259.     if (interactive)
  4260.         fputs(prompt, stderr);
  4261.     do {
  4262.         /* grab some input */
  4263.         if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4264.         == (char *) NULL) {
  4265.         /* end-of-file */
  4266.         if (interactive)
  4267.             (void) putc('\n', stderr);
  4268.         input_line[start] = '\0';
  4269.         inline_num++;
  4270.         if (start > 0)    /* don't quit yet - process what we have */
  4271.             more = FALSE;
  4272.         else
  4273.             return(1);  /* exit gnuplot */
  4274.         } else {
  4275.         /* normal line input */
  4276.         last = strlen(input_line) - 1;
  4277.         if (input_line[last] == '\n') {    /* remove any newline */
  4278.             input_line[last] = '\0';
  4279.             /* Watch out that we don't backup beyond 0 (1-1-1) */
  4280.             if (last > 0)
  4281.             --last;
  4282.             inline_num++;
  4283.         } else if (last + 1 >= MAX_LINE_LEN)
  4284.             int_error("Input line too long", NO_CARET);
  4285.  
  4286.         if (input_line[last] == '\\') {    /* line continuation */
  4287.             start = last;
  4288.             more = TRUE;
  4289.         } else
  4290.             more = FALSE;
  4291.         }
  4292.         if (more && interactive)
  4293.         fputs("> ", stderr);
  4294.     } while (more);
  4295.     }
  4296.     return(0);
  4297. }
  4298.  
  4299.  
  4300. do_shell()
  4301. {
  4302.     register char  *comspec;
  4303.     if ((comspec = getenv("COMSPEC")) == (char *) NULL)
  4304.     comspec = "\\command.com";
  4305. #ifdef _Windows
  4306.     if (WinExec(comspec, SW_SHOWNORMAL) <= 32)
  4307. #else
  4308. #ifdef DJGPP
  4309.     if (system(comspec) == -1)
  4310. #else
  4311.     if (spawnl(P_WAIT, comspec, NULL) == -1)
  4312. #endif
  4313. #endif
  4314.     os_error("unable to spawn shell", NO_CARET);
  4315. }
  4316.  
  4317. #else                /* MSDOS */
  4318. /* plain old Unix */
  4319.  
  4320. read_line(prompt)
  4321.     char           *prompt;
  4322. {
  4323.     int             start = 0;
  4324.     TBOOLEAN         more = FALSE;
  4325.     int             last = 0;
  4326.  
  4327. #ifndef READLINE
  4328.     if (interactive)
  4329.     fputs(prompt, stderr);
  4330. #endif                /* READLINE */
  4331.     do {
  4332.     /* grab some input */
  4333. #ifdef READLINE
  4334.     if (((interactive)
  4335.          ? rlgets(&(input_line[start]), MAX_LINE_LEN - start,
  4336.               ((more) ? "> " : prompt))
  4337.          : fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin))
  4338.         == (char *) NULL) {
  4339. #else
  4340.     if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4341.         == (char *) NULL) {
  4342. #endif                /* READLINE */
  4343.         /* end-of-file */
  4344.         if (interactive)
  4345.         (void) putc('\n', stderr);
  4346.         input_line[start] = '\0';
  4347.         inline_num++;
  4348.         if (start > 0)    /* don't quit yet - process what we have */
  4349.         more = FALSE;
  4350.         else
  4351.         return(1); /* exit gnuplot */
  4352.     } else {
  4353.         /* normal line input */
  4354.         last = strlen(input_line) - 1;
  4355.         if (input_line[last] == '\n') {    /* remove any newline */
  4356.         input_line[last] = '\0';
  4357.         /* Watch out that we don't backup beyond 0 (1-1-1) */
  4358.         if (last > 0)
  4359.             --last;
  4360.         } else if (last + 1 >= MAX_LINE_LEN)
  4361.         int_error("Input line too long", NO_CARET);
  4362.  
  4363.         if (input_line[last] == '\\') {    /* line continuation */
  4364.         start = last;
  4365.         more = TRUE;
  4366.         } else
  4367.         more = FALSE;
  4368.     }
  4369. #ifndef READLINE
  4370.     if (more && interactive)
  4371.         fputs("> ", stderr);
  4372. #endif
  4373.     } while (more);
  4374.     return(0);
  4375. }
  4376.  
  4377. #ifdef VFORK
  4378.  
  4379. do_shell()
  4380. {
  4381.     register char  *shell;
  4382.     register int    p;
  4383.     static int      execstat;
  4384.     if (!(shell = getenv("SHELL")))
  4385.     shell = SHELL;
  4386. #ifdef AMIGA_AC_5
  4387.     execstat = fexecl(shell, shell, NULL);
  4388. #else
  4389.     if ((p = vfork()) == 0) {
  4390.     execstat = execl(shell, shell, NULL);
  4391.     _exit(1);
  4392.     } else if (p == -1)
  4393.     os_error("vfork failed", c_token);
  4394.     else
  4395.     while (wait(NULL) != p)
  4396. #endif
  4397.         ;
  4398.     if (execstat == -1)
  4399.     os_error("shell exec failed", c_token);
  4400.     (void) putc('\n', stderr);
  4401. }
  4402. #else                /* VFORK */
  4403.  
  4404. #ifdef AMIGA_SC_6_1
  4405. do_shell()
  4406. {
  4407.     register char  *shell;
  4408.     if (!(shell = getenv("SHELL")))
  4409.     shell = SHELL;
  4410.  
  4411.     if (system(shell))
  4412.     os_error("system() failed", NO_CARET);
  4413.  
  4414.     (void) putc('\n', stderr);
  4415. }
  4416. #else                /* AMIGA_SC_6_1 */
  4417. #ifdef OS2
  4418. do_shell()
  4419. {
  4420.     register char  *shell;
  4421.     if (!(shell = getenv("COMSPEC")))
  4422.     shell = SHELL;
  4423.  
  4424.     if (system(shell) == -1 )
  4425.     os_error("system() failed", NO_CARET);
  4426.  
  4427.     (void) putc('\n', stderr);
  4428. }
  4429. #else                           /* ! OS2 */
  4430. #define EXEC "exec "
  4431. do_shell()
  4432. {
  4433.     static char     exec[100] = EXEC;
  4434.     register char  *shell;
  4435.     if (!(shell = getenv("SHELL")))
  4436.     shell = SHELL;
  4437.  
  4438.     if (system(strncpy(&exec[sizeof(EXEC) - 1], shell,
  4439.                sizeof(exec) - sizeof(EXEC) - 1)))
  4440.     os_error("system() failed", NO_CARET);
  4441.  
  4442.     (void) putc('\n', stderr);
  4443. }
  4444. #endif                          /* OS2 */
  4445. #endif                /* AMIGA_SC_6_1 */
  4446. #endif                /* VFORK */
  4447. #endif                /* MSDOS */
  4448. #endif                /* vms */
  4449.  
  4450. #ifdef _Windows
  4451. /* there is a system like call on MS Windows but it is a bit difficult to 
  4452.    use, so we will invoke the command interpreter and use it to execute the 
  4453.    commands */
  4454. int winsystem(s)
  4455. char *s;
  4456. {
  4457.     LPSTR comspec;
  4458.     LPSTR execstr;
  4459.     LPSTR p;
  4460.  
  4461.     /* get COMSPEC environment variable */
  4462. #ifdef WIN32
  4463.     char envbuf[81];
  4464.     GetEnvironmentVariable("COMSPEC", envbuf, 80);
  4465.     if (*envbuf == '\0')
  4466.         comspec = "\\command.com";
  4467.     else
  4468.         comspec = envbuf;
  4469. #else
  4470.     p = GetDOSEnvironment();
  4471.     comspec = "\\command.com";
  4472.     while (*p) {
  4473.         if (!strncmp(p,"COMSPEC=",8)) {
  4474.             comspec=p+8;
  4475.             break;
  4476.         }
  4477.         p+=strlen(p)+1;
  4478.     }
  4479. #endif
  4480.     /* if the command is blank we must use command.com */
  4481.     p = s;
  4482.     while ((*p == ' ') || (*p == '\n') || (*p == '\r'))
  4483.         p++;
  4484.     if (*p == '\0')
  4485.     {
  4486.         WinExec(comspec, SW_SHOWNORMAL);
  4487.         }
  4488.     else
  4489.     {
  4490.         /* attempt to run the windows/dos program via windows */
  4491.         if (WinExec(s, SW_SHOWNORMAL) <= 32)
  4492.         {
  4493.             /* attempt to run it as a dos program from command line */
  4494.             execstr = (char *) malloc(strlen(s) + strlen(comspec) + 6);
  4495.             strcpy(execstr, comspec);
  4496.             strcat(execstr, " /c ");
  4497.             strcat(execstr, s);
  4498.             WinExec(execstr, SW_SHOWNORMAL);
  4499.             free(execstr);
  4500.             }
  4501.         }
  4502.  
  4503.     /* regardless of the reality return OK - the consequences of */
  4504.     /* failure include shutting down Windows */
  4505.     return(0);        /* success */
  4506.     }
  4507. #endif
  4508.  
  4509. #endif /* THINK_C_2 */
  4510.